Menu Close

How To Add a Column in Pandas Dataframe ( 4 Ways )

Add a Column In Pandas Dataframe

In this article, you will learn everything about how to add a column in Pandas Dataframe.In the previous tutorials, we have seen a lot of Python Dataframe tutorials. In this guide various ways to insert a column in Pandas Dataframe.

What is Pandas Dataframe

Pandas Dataframe is basically a two-dimensional array that stores that in a form of rows and columns. As you can see below example to understand Python Dataframe.

How To Add a Column In Pandas Dataframe

Here we have covered a total of four ways to add a column in Pandas Dataframe with the help of the examples.

Declaring a new list as a column

In this method, we can define a new list that represents the new column in the existing Pandas Dataframe.

Example: Add A Column In Pandas Dataframe


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# defining new list that reprsent the value 
address = ["Delhi", "Lucknow", "Mumbai", "Bangalore"]

# add new column in existing dataframe
df["Address"] = address

#print Dataframe
print(df)

Output


            Name  age           Occupation                   Skills    Address
0  Vishvajit Rao   23            Developer                   Python      Delhi
1           John   33  Front End Developer                  Angular    Lucknow
2       Harshita   21               Tester                 Selenium     Mumbai
3          Mohak   30           Full Stack  Python, React and MySQL  Bangalore

Using DataFrame.insert() Method

Pandas DataFrame provides the best method insert() that is used to insert a new column at any position in DataFrame, not the end. Let’s see how can we do that.

Note:- Position starts from 0.

Example: Insert A Column In Pandas Dataframe


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# defining new list that reprsent the value 
address = ["Delhi", "Lucknow", "Mumbai", "Bangalore"]

# add address column at second position
df.insert(1, "Address", address, True)

#print Dataframe
print(df)

Output


            Name    Address  age           Occupation                   Skills
0  Vishvajit Rao      Delhi   23            Developer                   Python
1           John    Lucknow   33  Front End Developer                  Angular
2       Harshita     Mumbai   21               Tester                 Selenium
3          Mohak  Bangalore   30           Full Stack  Python, React and MySQL

Using DataFrame.assign() method

Pandas DataFrame also provides another method to add a column in Pandas DataFrame called assign(). The assign() method returns a new DataFrame after adding a new column to the existing DataFrame.

Example: Add Column In Pandas Dataframe


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# defining new list that reprsent the value 
address = ["Delhi", "Lucknow", "Mumbai", "Bangalore"]

# add address column
df2 = df.assign(Address=address)

#print Dataframe
print(df2)

Output


            Name  age           Occupation                   Skills    Address
0  Vishvajit Rao   23            Developer                   Python      Delhi
1           John   33  Front End Developer                  Angular    Lucknow
2       Harshita   21               Tester                 Selenium     Mumbai
3          Mohak   30           Full Stack  Python, React and MySQL  Bangalore

Using Python Dictionary

We can also use Python Dictionary to add columns in Pandas DataFrame.Use existing column values as the values and their keys will be the value of the new column.

Example: Add a Column In Pandas Dataframe


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# defining new list that reprsent the value 
address = ["Delhi", "Lucknow", "Mumbai", "Bangalore"]

# add address column
data = {
    "Noida": "Vishvajit Rao", "Bangalore": "John", "Harshita": "Pune", "Mohak": "Delhi"
}

# adding new column address
df["Address"] = data

# print
print(df)

Output


            Name  age           Occupation                   Skills    Address
0  Vishvajit Rao   23            Developer                   Python      Noida
1           John   33  Front End Developer                  Angular  Bangalore
2       Harshita   21               Tester                 Selenium   Harshita
3          Mohak   30           Full Stack  Python, React and MySQL      Mohak

Related Articles:-

Conclusion

So, in this article, we have seen all about how to add a column in Pandas DataFrame with the help of various examples. Here we have explored a total of four ways to insert a new column in Pandas Dataframe.

You can use any of them as per your choice to add a column in Pandas DataFrame.I hope this article will help you, If you like this article, please share and keep visiting for further Python Pandas tutorials.

Thanks for your valuable time …. 👏👏

How to Convert Excel to JSON in Python
Python Pandas Tutorial

Related Posts