0

I'm trying to add few list to an existing data. I'm not sure if I can do in easy and efficient way.

# df is already created with many columns.
# The lists lst1, lst2, lst3, lst4 will be added to df.

df['newcol1']=lst1
df['newcol2']=lst2
df['newcol3']=lst3
df['newcol4']=lst4

This works fine. Is there any better way like this?

# I tried following methods
df[['newcol1','newcol2','newcol3','newcol4']]=[lst1,lst2,lst3,lst4]
df[['newcol1','newcol2','newcol3','newcol4']]=list(zip(lst1,lst2,lst3,lst4))
df[['newcol1','newcol2','newcol3','newcol4']]=list(map(list,zip(lst1,lst2,lst3,lst4)))

All these methods do not work.

df[['newcol1','newcol2','newcol3','newcol4']]=?? # What should I write here?

I don't want to create new dataset and do pd.concat or use some apply function.

There must be a better way for df[['newcol1','newcol2','newcol3','newcol4']].

Tamal
  • 45
  • 5
  • See this post https://stackoverflow.com/questions/39050539/how-to-add-multiple-columns-to-pandas-dataframe-in-one-assignment – Kurt Kline May 15 '20 at 17:59
  • Thank you all for quick response. Keep it up!!! df['newcol1'],df['newcol2'],df['newcol3'], df['newcol4']=[lst1,lst2,lst3,lst4]. I think this is the best one liner code. – Tamal May 15 '20 at 18:19

0 Answers0