Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pandas assign multiple columns at once

df = pd.DataFrame({'col1':[0,1,2], 'col2':[0,1,2], 'col3':[0,1,2]})

df
   col1  col2  col3
0     0     0     0
1     1     1     1
2     2     2     2

df['col1'], df['col2'], df['col3'] = zip(*df.apply(lambda r: (1, 2, 3), axis=1))

df
   col1  col2  col3
0     1     2     3
1     1     2     3
2     1     2     3
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #pandas #assign #multiple #columns
ADD COMMENT
Topic
Name
7+5 =