Add Empty Column to dataframe in Pandas : 3 Methods

Add Empty Column to dataframe in Pandas

Pandas dataframe allows you to manipulate the datasets easily. Once you have converted any datasets to pandas dataframe then you can remove, add, modify the rows or columns e.t.c easily. In this tutorial, you will learn how to add an empty column to an existing dataframe in pandas using various methods.

Methods to add an empty column to dataframe in pandas

In this section, you will learn all the methods to add an empty column to an existing dataframe. But before that let’s create the sample dataframe in pandas.

Run the below lines of code to create the sample dataframe.

import pandas as pd
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
"country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
print(df)

Output

    name  age  country
0    Rob   23      USA
1    Bam   25       UK
2   Maya   26   France
3  Rahul   32  Germany

Method 1: Using the square bracket

The first and easy method to add an empty column to the dataframe is the use of columns name inside the square bracket. The syntax for it is below.

your_dataframe["column_name]

Let’s say I want to add an empty column with the column name ” dialing_code” then I will execute the below lines of code.

import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
        "country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df["dialing_code"] =np.nan
print(df)

Output

    name  age  country  dialing_code
0    Rob   23      USA           NaN
1    Bam   25       UK           NaN
2   Maya   26   France           NaN
3  Rahul   32  Germany           NaN

You can see in all the columns I have filled the NaN value. You can also fill in the blank values using the df[“dialing_code”] = “”. Here I have also used the numpy.nan to add NaN values in each row.

Method 2: Using the pd.Series() constructor

Instead of using the np.nan you can also use the pandas.Series() constructor to add the empty column to dataframe in pandas.

df['dialing_code'] = pd.Series(dtype='int')
import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
        "country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df['dialing_code'] = pd.Series(dtype='int')
print(df)

Output

  name  age  country  dialing_code
0    Rob   23      USA           NaN
1    Bam   25       UK           NaN
2   Maya   26   France           NaN
3  Rahul   32  Germany           NaN

Method 3:  Using the join() function

In this method, you will first create a dataframe with the new column name you want to add and then join this dataframe with the existing dataframe. The dataframe.join() method will complete the addition of the column.

Run the below lines of code.

import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rahul"],"age":[23,25,26,32],
        "country":["USA","UK","France","Germany"]}
df = pd.DataFrame(data)
df1 = pd.DataFrame(columns=["dialing_code"])
df = df.join(df1, how="outer")
print(df)

Output

  name  age  country dialing_code
0    Rob   23      USA          NaN
1    Bam   25       UK          NaN
2   Maya   26   France          NaN
3  Rahul   32  Germany          NaN

Conclusion

Pandas have many inbuilt functions that allow you to manipulate datasets. If you want to add an empty column to the dataframe in pandas then the above methods are the solution. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner