Home > Blockchain >  Pandas - Add columns from a list of strings
Pandas - Add columns from a list of strings

Time:12-22

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!

CodePudding user response:

assign directly

df[newcols] = ""

CodePudding user response:

You can reindex with the columns union:

df2 = df1.reindex(columns=df.columns.union(newcols))

CodePudding user response:

Use concat:

pd.concat([df, pd.DataFrame(columns=['D','E','F'])], axis=1)
  • Related