How to add new columns to existing DataFrame in Pandas?

To add new columns to an existing DataFrame in Pandas, you can use the DataFrame.assign() method, or directly access the DataFrame columns attribute and assign it a new column name and value. You can also use the DataFrame.insert() method to add a new column at a specific location. Additionally, you can use the DataFrame.assign() method to assign multiple columns at once by providing a dict of column names and values.


There are three common ways to create a new pandas DataFrame from an existing DataFrame:

Method 1: Create New DataFrame Using Multiple Columns from Old DataFrame

new_df = old_df[['col1','col2']].copy()

Method 2: Create New DataFrame Using One Column from Old DataFrame

new_df = old_df[['col1']].copy()

Method 3: Create New DataFrame Using All But One Column from Old DataFrame

new_df = old_df.drop('col1', axis=1)

The following examples show how to use each method with the following pandas DataFrame:

import pandas as pd

#create DataFrame
old_df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                       'points': [18, 22, 19, 14, 14, 11, 20, 28],
                       'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                       'rebounds': [11, 8, 10, 6, 6, 7, 9, 12]})

#view DataFrame
print(old_df)

Example 1: Create New DataFrame Using Multiple Columns from Old DataFrame

The following code shows how to create a new DataFrame using multiple columns from the old DataFrame:

#create new DataFrame from existing DataFrame
new_df = old_df[['points','rebounds']].copy()

#view new DataFrame
print(new_df)

   points  rebounds
0      18        11
1      22         8
2      19        10
3      14         6
4      14         6
5      11         7
6      20         9
7      28        12

#check data type of new DataFrame
type(new_df)

pandas.core.frame.DataFrame

Notice that this new DataFrame only contains the points and rebounds columns from the old DataFrame.

Note: It’s important to use the copy() function when creating the new DataFrame so that we avoid any if we happen to modify the new DataFrame in any way.

Example 2: Create New DataFrame Using One Column from Old DataFrame

The following code shows how to create a new DataFrame using one column from the old DataFrame:

#create new DataFrame from existing DataFrame
new_df = old_df[['points']].copy()

#view new DataFrame
print(new_df)

   points
0      18
1      22
2      19
3      14
4      14
5      11
6      20
7      28

#check data type of new DataFrame
type(new_df)

pandas.core.frame.DataFrame

Example 3: Create New DataFrame Using All But One Column from Old DataFrame

The following code shows how to create a new DataFrame using all but one column from the old DataFrame:

#create new DataFrame from existing DataFrame
new_df = old_df.drop('points', axis=1)

#view new DataFrame
print(new_df)

  team  assists  rebounds
0    A        5        11
1    A        7         8
2    A        7        10
3    A        9         6
4    B       12         6
5    B        9         7
6    B        9         9
7    B        4        12

#check data type of new DataFrame
type(new_df)

pandas.core.frame.DataFrame

Notice that this new DataFrame contains all of the columns from the original DataFrame except the points column.

The following tutorials explain how to perform other common tasks in Python:

x