Adding new columns to a DataFrame in Pandas
Start your free 7-days trial now!
To add new columns to a DataFrame in Pandas, use the DataFrame's assign(~)
method.
Adding a column of constants
Consider the following DataFrame:
df
A B0 3 51 4 6
To append a new column whose values are a single constant:
A B C0 3 5 201 4 6 20
Here, note the following:
the column name is specified by the keyword argument (e.g.
C=
).the source
df
is left intact, and a new DataFrame is returned.
To add a new column in place (i.e. modify df
directly rather than returning a new DataFrame):
df["C"] = 20df
A B C0 3 5 201 4 6 20
Adding a column using an array
Consider the following DataFrame:
df
A B0 3 51 4 6
To append a new column using an array:
A B C0 3 5 71 4 6 8
Note that the length of the new column must match up with that of the source DataFrame.
Adding a column using a function
Consider the following DataFrame:
df
A B0 3 51 4 6
To append a new column C
, which is the sum of columns A
and B
:
A B C0 3 5 81 4 6 10
Note that, whenever possible, opt for column-arithmetics instead for performance:
df["C"] = df["A"] + df["B"]df
A B C0 3 5 81 4 6 10
Adding multiple columns
Consider the following DataFrame:
df
A B0 3 51 4 6
To append multiple columns in one-go, just specify multiple keyword arguments:
A B C D0 3 5 20 101 4 6 20 20
Notice how the column labels are specified using keyword arguments (C
and D
).