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!

1
  • You didn't accept any of the answers. That's not fair. Commented Dec 21, 2022 at 23:25

3 Answers 3

2

You can reindex with the columns union:

df2 = df1.reindex(columns=df.columns.union(newcols))
Sign up to request clarification or add additional context in comments.

Comments

1

assign directly

df[newcols] = ""

1 Comment

Thanks! It worked fine. Felling kind of dumd right know hahaha!
1

Use concat:

pd.concat([df, pd.DataFrame(columns=newcols)], axis=1)

2 Comments

The headers will not always be "D,E,F", it was just an example. That's why i need to use the list.
Just use the list name instead. I edited my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.