Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign values to multiple columns in Pandas [duplicate]

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
like image 755
SpanishBoy Avatar asked Dec 03 '15 19:12

SpanishBoy


1 Answers

Looks like solution is simple:

df['col2'], df['col3'] = zip(*[(2,3), (2,3), (2,3)])
like image 65
SpanishBoy Avatar answered Sep 23 '22 14:09

SpanishBoy