Python for Beginners: Pandas Insert Columns into a DataFrame in Python :

Python for Beginners: Pandas Insert Columns into a DataFrame in Python
by:
blow post content copied from  Planet Python
click here to view original post


We use a pandas dataframe to store and manipulate tabular data in python. In this article, we will discuss how to insert a new column into the pandas dataframe using the insert() method.

The Pandas insert() Method

The insert() method is used to insert a column into a dataframe at a specific position. It has the following syntax.

DataFrame.insert(loc, column, value, allow_duplicates=_NoDefault.no_default)

Here,

  • The loc parameter takes the index at which the new column is inserted as its input argument.
  • The column parameter takes the column name as its input.
  • The value parameter takes a list or pandas series as values for the specified column.
  • The allow_duplicates parameter is used to decide if we can insert duplicate column names into the dataframe. By default, the insert() method raises a ValueError exception if the dataframe contains a column with the same name that we are trying to insert. If you want to insert duplicate column names into the pandas dataframe, you can set the allow_duplicates parameter to True.

Pandas Insert a Column at The Beginning of a DataFrame

To insert a column at the beginning of a dataframe, we can use the insert() method. Here, we will set the loc parameter to 0 so that the new column is inserted at the beginning. You can observe this in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
df.insert(0,"Name", ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"])
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
     Name  Roll  Maths  Physics  Chemistry
0  Aditya     1    100       80         90
1    Joel     2     80      100         90
2     Sam     3     90       80         70
3   Chris     4    100      100         90
4    Riya     5     90       90         80
5    Anne     6     80       70         70

In this example, we first converted a list of dictionaries to a dataframe using the DataFrame() function. Then, we inserted the Name column in the created dataframe at index 0 using the insert() method. For this, we passed the value 0 as the first input argument, the string "Name" as the second input argument and the list of values as the third input argument to the insert() method.

Insert Column at The End of a DataFrame in Python

To insert a column at the end of the dataframe, we can directly assign the column values to the column name in the dataframe as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
df["Name"]= ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"]
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
   Roll  Maths  Physics  Chemistry    Name
0     1    100       80         90  Aditya
1     2     80      100         90    Joel
2     3     90       80         70     Sam
3     4    100      100         90   Chris
4     5     90       90         80    Riya
5     6     80       70         70    Anne

In the above example, we have used the indexing operator to insert a new column at the end of a dataframe.

Instead of the above approach, we can also use the insert() method to insert a column at the end. For this, we will use the following steps.

  • First, will obtain the list of column names using the columns attribute of the dataframe. The columns attribute contains a list of column names.
  • Next, we will use the len() function to find the total number of columns in the dataframe. Let it be numCol.
  • Once, we get the number of columns in the dataframe, we know that the current columns exist at the positions 0 to numCol-1. Hence, we will insert the new column to the dataframe at the index numCol using the insert() method.

After execution of the above steps, we can insert a column at the end of the dataframe as shown in the following example.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
numCol=len(df.columns)
df.insert(numCol,"Name", ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"])
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
   Roll  Maths  Physics  Chemistry    Name
0     1    100       80         90  Aditya
1     2     80      100         90    Joel
2     3     90       80         70     Sam
3     4    100      100         90   Chris
4     5     90       90         80    Riya
5     6     80       70         70    Anne

Pandas Insert Column at a Specific Index in a DataFrame

To insert a column at a specific position in the dataframe, you can use the insert() method as shown below.

import pandas as pd
myDicts=[{"Roll":1,"Maths":100, "Physics":80, "Chemistry": 90},
        {"Roll":2,"Maths":80, "Physics":100, "Chemistry": 90},
        {"Roll":3,"Maths":90, "Physics":80, "Chemistry": 70},
        {"Roll":4,"Maths":100, "Physics":100, "Chemistry": 90},
        {"Roll":5,"Maths":90, "Physics":90, "Chemistry": 80},
        {"Roll":6,"Maths":80, "Physics":70, "Chemistry": 70}]
df=pd.DataFrame(myDicts)
print("The original dataframe is:")
print(df)
df.insert(2,"Name", ["Aditya","Joel", "Sam", "Chris", "Riya", "Anne"])
print("The mofified dataframe is:")
print(df)

Output:

The original dataframe is:
   Roll  Maths  Physics  Chemistry
0     1    100       80         90
1     2     80      100         90
2     3     90       80         70
3     4    100      100         90
4     5     90       90         80
5     6     80       70         70
The mofified dataframe is:
   Roll  Maths    Name  Physics  Chemistry
0     1    100  Aditya       80         90
1     2     80    Joel      100         90
2     3     90     Sam       80         70
3     4    100   Chris      100         90
4     5     90    Riya       90         80
5     6     80    Anne       70         70

In this example, we have inserted the Name column at index 2 of the input dataframe using the insert() method.

Conclusion

In this article, we discussed different ways to insert a column in a pandas dataframe. To learn more about python programming, you can read this article on how to create an empty dataframe in python. You might also like this article on working with XML files in Python.

I hope you enjoyed reading this article. Stay tuned for more informative articles.

Happy Learning!

The post Pandas Insert Columns into a DataFrame in Python appeared first on PythonForBeginners.com.


March 06, 2023 at 07:30PM
Click here for more details...

=============================
The original post is available in Planet Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce