Assign values to multiple columns in Pandas

SpanishBoy picture SpanishBoy · Dec 3, 2015 · Viewed 23.1k times · Source

I have follow simple DataFrame - df:

   0
0  1
1  2
2  3

Once I try to create a new columns and assign some values for them, as example below:

df['col2', 'col3'] = [(2,3), (2,3), (2,3)]

I got following structure

   0 (col2, col3)
0  1    (2, 3)
1  2    (2, 3)
2  3    (2, 3)

However, I am looking a way to get as here:

   0 col2, col3
0  1    2,   3
1  2    2,   3
2  3    2,   3

Answer

SpanishBoy picture SpanishBoy · Dec 3, 2015

Looks like solution is simple:

df['col2'], df['col3'] = zip(*[(2,3), (2,3), (2,3)])