Pandas >> How to Add Columns to an Existing DataFrame

2021-11-25 Pandas

Table of Contents

[Pandas] How to add columns to a DataFrame

In this article, we will talk about how to add columns to an existing DataFrame.

Firstly, we prepare data for demonstration.

Preparing data

import pandas as pd

df = pd.DataFrame({
    "name": ["Kevin", "Jack", "Mary"],
    "score": [80, 90, 95],
    "class": ["A", "B", "A"],
    "sex": ["M", "M", "F"]
}, index=["K", "J", "M"])

df

[Pandas] How to add columns to a DataFrame

The way to add column

Here we will introduce four methods to add columns to DataFrame.

  • Add column by specifying new column name in brackets.
  • Concatenate the column Series to the existing DataFrame using concat method.
  • Insert column in any position using insert method of DataFrame.
  • Add new column or update existing column using assign method of DataFrame.

Add column by specifying new column name in brackets after DataFrame

We can specify new column name in brackets to add new column to DataFrame. The right side of = can be a Series or a scalar constant or an array.

Assign a constant to a new column

For example, we can add a new column named “newCol1” by assigning a constant.

df["newCol1"] = "a"
df

[Pandas] How to add columns to a DataFrame

Assign an array to a new column

We can also add a new column named “newCol2” by assigning an array.

df["newCol2"] = [1, 2, 3]
df

[Pandas] How to add columns to a DataFrame

Assign a Series to a new column

We can also add a new column named “newCol3” by assigning an Series.

df["newCol3"] = pd.Series([4,5,6], index=["K", "J", "M"])
df

[Pandas] How to add columns to a DataFrame

The Series can be created based existing columns.

df["newCol4"] = df["newCol2"] + df["newCol3"]
df

[Pandas] How to add columns to a DataFrame

Add multiple columns to a DataFrame

We can also use apply method of DataFrame to add multiple columns at one time.

df[["newCol5", "newCol6"]] = df.apply(lambda x: pd.Series({"newCol5": f"Hello {x['name']}", "newCol6": f"Bye {x['name']}"}), axis=1)
df

[Pandas] How to add columns to a DataFrame

Concatenate a Series to a DataFrame horizontally

We can also use concat method of Pandas to concatenate a Series to a DataFrame.

newCol = pd.Series(["OK Kevin", "OK Jack", "OK Mary"], index=["K", "J", "M"], name="newCol7")
df = pd.concat([df, newCol], axis=1)
df

[Pandas] How to add columns to a DataFrame

We can also concatenate multiple Series to a DataFrame.

newCol2 = pd.Series(["OK2 Kevin", "OK2 Jack", "OK2 Mary"], index=["K", "J", "M"], name="newCol8")
newCol3 = pd.Series(["OK3 Kevin", "OK3 Jack", "OK3 Mary"], index=["K", "J", "M"], name="newCol9")
df = pd.concat([df, newCol2, newCol3], axis=1)
df

[Pandas] How to add columns to a DataFrame

Insert a Series to specified position of DataFrame

We can also use insert method of DataFrame to insert a Series to the specified position of a DataFrame.

df.insert(len(df.columns), "newCol10", 1)
df

[Pandas] How to add columns to a DataFrame

Use assign to add new columns

We can also use assign method of DataFrame to append new columns to a DataFrame.

df = df.assign(newCol11=2)
df

[Pandas] How to add columns to a DataFrame

Conclusions

In this article, we introduced four methods to add new columns to a DataFrame.

  • brackets syntax df[new column] = ・・・
  • insert method of DataFrame
  • concat method of PandasHow
  • assign method of DataFrame

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us