Through this Python tutorial, We will be covering the maximum possible methods to add or append a new column to an existing Pandas DataFrames. At the end of this tutorial, We will get a clear idea about how to add new columns to an existing Pandas DataFrames in Python.
There are various methods to add or append new columns to a Pandas dataframe like:
- Append or add a new column to the existing Pandas DataFrame using a List method
- Append or add a new column to an existing Pandas DataFrame using the assign() method
- Append or add a new column to an existing Pandas DataFrame using the insert() method
- Append or add a new column to an existing Pandas DataFrame using the pd.Series() method
- Append or add a new column to an existing Pandas DataFrame using the pd.contact() method
Also, I will show you how to append or add a new column with null values to the existing Pandas DataFrame in Python.
And also, we will see, how to append or add a constant value to a new column to the existing Pandas DataFrame in Python.
Pandas add a new column to an existing DataFrame
Pandas DataFrame is a table where we can store our data in the form of rows and columns. We can even add new columns to the existing Pandas DataFrames in Python.
To dive into the examples, we need to create a DataFrame using pandas.
Create a DataFrame using Pandas in Python
Let us create our own Pandas DataFrame with multiple rows so that we can further add extra columns to the existing Pandas DataFrames in Python.
- Here we have created a dictionary in Python of employee data that has the names of the employees, experiences, and roles of the employees from different organizations.
- Later it is passed to the “pandas.DataFrame” function to convert it to a data frame or a table i.e in the form of rows and columns.
#Importing the necessary libraries
import numpy as np
import pandas as pd
#Create a dictionary which has employee names, their experience, company and roles
data_dict={"Names":["Kelvin", "John", "smith", "Robin","Williams","Nick","Anyy","Messi","Jonas","Xavier"],
"Experience":[13,7,np.nan,9,0,12,21,3,9,17],
"Company": ["Google","Amazon","Google","Flipkart","Amazon","Google","Flipkart","Amazon","Google","Flipkart"],
"Role": ["IT Analyst","Software Engineer","Software Engineer","Data Analyst","Data Engineer","Data Scientist","ML Engineer","ML Engineer","ML Engineer","Data Scientist"]}
#Create a DataFrame using Pandas
Employee_data=pd.DataFrame(data_dict)
Employee_data
- Below is the pandas DataFrame that we have created in Python which has the employee names, their company name, roles, and experience in the IT Industry.
- The column names of the pandas DataFrame are given to the dictionary ‘data_dict’ as keys and the rest are values in Python.
Now, let us check different methods to add a new column to an existing pandas dataframe.
Add a column to an existing Pandas DataFrame using a List
We can append a new column to the existing pandas DataFrame using the list in Python. Here in the below code, we have created a list that has the ages of all the employees and stored the list in a variable “Employee_Age“.
And then added a new column named “Age ” to the existing DataFrame “Employee_data“.In the second line of code, we are assigning all the age values in the list to the new column “Age“.
# Create a new list that has all the employees ages in employee_age
Employee_Age=[31,23,42,53,29,43,51,33,29,30]
#Adding new column i.e Employee_Age to the existing pandas DataFrame Employee_data
Employee_data["Age"]=Employee_Age
Employee_data
In the Below output image, We can see that a new column called “Age” is added to the existing pandas DataFrame in Python.
Here We have covered the basic and most commonly used approach to add a new column to the DataFrame in Python.
Add a column to an existing Pandas DataFrame using the assign() method
There are many ways to append a new column to the existing pandas DataFrame. One of the most widely used functions to add or append a new column in Python is “assign()“.
The assign() method in Python adds the column at the end i.e the new column that is going to be added using assign() will be the last column in the entire dataset or pandas dataframe.
#Add new column (Age) to the existing DataFrame in python
Employee_data.assign(Age=[31,23,42,53,29,43,51,33,29,30])
In the below output image, We can see that a new column called “Age” is added to the existing pandas DataFrame “Employee_data” in Python.
This is how to add a new column to an existing Pandas DataFrame using assign() method.
Add a column to an existing Pandas DataFrame using the insert() method
One of the most widely used functions in Python is “insert()” to append or add new columns to the existing Pandas DataFrames or large datasets.
- Using the “insert()” method, we can add or append the new column at any index position in the existing pandas DataFrame in Python.
- Within the insert() method in the below code, We have passed 3 parameters i.e first parameter is the index position, the second parameter is the new column name and the third parameter is column values.
#Add new column (Age) at index position 2 to the existing DataFrame in python
Employee_data.insert(2,"Age",[31,23,42,53,29,43,51,33,29,30])
Employee_data
In the below output image, we can see that the new column named “Age ” has been added at the 2nd index. This way we can even add new columns to the existing DataFrame at different indexes in Python.
This is all about adding a new column to the existing Pandas DataFrame using ‘insert()’ method.
Add a column to an existing Pandas DataFrame using the pd.Series() method
Pandas Series is a one-dimensional array. We will create a pandas series using the pd.Series() which is basically our new column and then we can append this Series i.e new column to the existing Pandas DataFrame in Python.
- Here in the below code, we have created a Series “Employee_Age” to store the ages of all the employees in Python.
- The “pd.Series()” creates a one-dimensional array to store the values of ages here. Then we created a new column “Age” and assigned the values in the Series to it.
# Create a new series that has all the employees ages in "employee_age"
Employee_Age=pd.Series([31,23,42,53,29,43,51,33,29,30])
#Adding new column i.e Employee_Age to the existing pandas DataFrame Employee_data
Employee_data["Age"]=Employee_Age.values
Employee_data
In the Below output image, We can see that a new column called “Age” is added to the existing Pandas DataFrame “Employee_data” in Python.
This is all about adding a new column to the existing Pandas DataFrame using ‘pd.Series()’ method.
Add a column to an existing Pandas DataFrame using the pd.contact() method
The inbuilt function in Python “pd.concat()”, can be used to add a new column to the existing pandas DataFrame in Python.
- Here in the below code, we have created a Series “Employee_Age” to store the ages of all the employees in Python. The “pd.Series” creates a one-dimensional array to store the values of ages here.
- Then we called the pd.concat() method and the first parameter we passed in it is the DataFrame or dataset and the second parameter is the new column with the values and axis=1 defines that the new series should be added vertically to the DataFrame in python.
# Create a Series that has all the employees ages in "Employee_age"
Employee_Age=pd.Series([31,23,42,53,29,43,51,33,29,30])
#Adding new column to the existing Pandas DataFrame using pd.concat()
pd.concat([Employee_data,Employee_Age.rename("Age")],axis=1)
In the below output image, we can observe that a new column called “Age” is added to the existing pandas DataFrame “Employee_data” in Python.
This is all about adding a new column to the existing Pandas DataFrame using the ‘pd.concat()’ method.
Add a new column with null values to an existing DataFrame
We can even add a new column with null values to the existing DataFrame in Python.
- In the below code, we have created a new column “Age” and directly appended the null values to the new column that is created.
- The “np.nan” stands for null values or not available data in Python.
# Create and append a new column "Age" with null values
Employee_data["Age"]=np.nan
Employee_data
In the Below output image, We can see that a new column called “Age” which has null values has been added to the existing pandas DataFrame “Employee_data” in Python.
This is about adding a new column with null values to an existing DataFrame in Python.
Add a constant value to a new column to an existing DataFrame
We can even add a new column with a constant value to the existing DataFrame in Python.
- In the below code, we have created a new column “Age” and directly appended some constant value to the new column that is created.
# Create a new column "Age" with constant values and append it to DataFrame
Employee_data["Age"]=32
Employee_data
In the below output image, we can observe that a new column called “Age” with a constant value of 32 has been added to the existing Pandas DataFrame “Employee_data” in Python.
This is about adding a new column with a constant value to an existing DataFrame in Python
Conclusion
Through this Python Pandas tutorial, We have covered all the possible methods to append or add a new column to the existing Pandas DataFrames like pd.concat(), DataFrame.assign(), DataFrame.insert(), using list, Series approaches, etc…
Also, we saw:
- Add a new column with null values to an existing DataFrame in Python
- Add a constant value to a new column to an existing DataFrame in Python
You may also like:
- How to convert pandas DataFrame into JSON in Python
- How to convert a list to DataFrame in Python
- get unique values in Pandas DataFrame
- How to Subset a DataFrame in Python
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.