1

I want to add columns from a list.

newcols = ['D', 'E', 'F']

df1 = (How it is)

A B C
10 15 5
11 14 9

df2 = (How i want it to be)

A B C D E F
10 15 5
11 14 9

Do you guys know how to get this done ?? Best Regards!

3 Answers3

2

You can reindex with the columns union:

df2 = df1.reindex(columns=df.columns.union(newcols))
mozway
  • 194,879
  • 13
  • 39
  • 75
1

Use concat:

pd.concat([df, pd.DataFrame(columns=newcols)], axis=1)
SomeDude
  • 13,876
  • 5
  • 21
  • 44
1

assign directly

df[newcols] = ""