In Python, we can add a new column to the existing DataFrame using Pandas in 6 following ways -
- By declaring a new List as a column
- Using Dataframe.insert() method
- Using Dataframe.assign() method
- Using a dictionary
- Using Pandas.concat() method
- Using loc[]
Let's have a look at each one of them and understand how they can be used to add a new column to the existing Pandas Dataframe with Python examples.
By declaring a new List as a column
Assigning a list is the simplest way to create and add a column to the dataframe in pandas.
Example 1 - By declaring a new List as a column
Let's declare a list and add it to our dataframe as a new column in pandas.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # List - for column maths = [99, 59, 45, 90] # 'Maths' as column name connected to the list dataframe_1['Maths'] = maths # Result After Adding Column print(dataframe_1)
The Output will be -

Note: The length of the list must be equal to the length of the index column; otherwise, the compiler will throw a ValueError -
ValueError: Length of values (1) does not match the length of index (4)
Using Dataframe.insert() method
The dataframe.insert() method allows us to add columns at any desired index, not only at the end of our dataframe in pandas. Additionally, it provides many alternatives for adding column values.
The Syntax is -
dataframe.insert(column_index, column_name_in_string, column_content_list, boolean)
If the dataframe has a column with the same name already present, then the column we are trying to add will only be added if the boolean value is True.
Example 1 - Using Dataframe.insert() method
We will add a new column using the dataframe.insert() method in this example.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # insert() method - 'Maths' as column name dataframe_1.insert(2, "Maths", [99, 59, 45, 90], False) # Result After Adding Column print(dataframe_1)
The Output will be -

As we can see from the output above, the column 'Maths' is placed at index #2 (The column numbering in Pandas dataframe starts with 0).
Example 2 - Using Dataframe.insert() method
In this example, we will try to add a column with the name that's already present in the dataset.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # insert() method - 'English' as column name dataframe_1.insert(2, "English", [99, 59, 45, 90], True) # Result After Adding Column print(dataframe_1)
The Output will be -

If we had set the boolean value in the dataframe.insert() method in our above example as False, then we would have got ValueError -
ValueError: cannot insert English, already exists.
Using Dataframe.assign() method
The dataframe.assign() method creates a new dataframe by adding a column to the existing dataframe.
We can ignore the column's index to be added or override the contents of an existing column.
Example 1 - Using Dataframe.assign() method
Let's look at how we can use the dataframe.assign() method in the following code.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # assign() method - 'Maths' as column name # assign() method creates a new dataframe # so we store it back in our previous dataframe dataframe_1 = dataframe_1.assign(Maths=[99, 59, 45, 90]) # Result After Adding Column print(dataframe_1)
The Output will be -

Using a dictionary
We can use a Python dictionary to add a new column to a pandas DataFrame. We can utilize an existing column's keys-values as the values (or key) for a new column's relevant values.
Let's see how we can add a column to Panda's existing dataframe using a dictionary.
Example 1 - Using a dictionary
In the following code, we will define a dictionary with the existing column's key-values and their respective value pairs as the values for our new column.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # Define a dictionary with the existing column's key values and # their respective value pairs as the values for our new column. maths = { 99: 'Tom', 79: 'Stacy', 45: 'Kevin', 90: 'Ron' } dataframe_1['Maths'] = maths # Result After Adding Column print(dataframe_1)
The Output will be -

If we get NaN values in the newly added column as shown in the image below, we can use fillna in our code to add a column to our existing Pandas Dataframe. Let's use and see it in the example below.

Example 2 - Using a dictionary
In the following code, we will define a dictionary with the existing column's key-values and their respective value pairs as the keys for our new column. We will use fillna to add dictionary content as a new column.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # Define a dictionary with the existing column's key values and # their respective value pairs as the keys for our new column. maths = { 'Tom': 99, 'Stacy': 79, 'Kevin': 45, 'Ron': 90 } # Using fillna to add dictionary content as a new column dataframe_1['Maths'] = {} dataframe_1.Maths = dataframe_1.Maths.fillna(dataframe_1.Name.map(maths)) # Result After Adding Column print(dataframe_1)
The Output will be -

Using Pandas.concat() method
We can use pandas.concat() method to add(concatenate) a new column to a DataFrame. This method returns a new DataFrame with the concatenated data.
The pandas.concat() method will concatenate the Series using the index with the original DataFrame.
- If the indices of the items to be concatenated match, we can use concat() method in most situations.
- If indices do not match, the result will contain all indices for every object.
Example - Using Pandas.concat() method
Let's understand pandas.concat() method with the code below.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # Creating a new series acc. to index from our dataframe maths = pd.Series([99, 79, 45, 90], index=dataframe_1.index) # concat() method - 'Maths' as column name # concat() method creates a new dataframe # so we store it back in previous dataframe dataframe_1 = pd.concat([dataframe_1, maths.rename('Maths')], axis=1) print(dataframe_1)
The Output will be -

The index is one of the most challenging aspects of adding new columns to DataFrames. Suppose the new column's index has no special significance, and we do not want it to be considered during insertion, in that case, we can indicate that the index of the Series is identical to the index of the DataFrame.
Using loc[]
Using pandas loc[], we can access rows and columns by labels or names, but we can also add new columns to a pandas DataFrame using this method.
The loc[] uses the first parameter as rows and the second parameter as columns; thus, we can use the second parameter to add a new column.
NOTE: Due to performance issues, we avoid using loc[] to add a column to Pandas Dataframe.
Example - Using loc[]
Let's look at how we can use loc[] to add a new column to our existing Pandas Dataframe in the code below.
Here is the Python Code -
# Importing - pandas import pandas as pd # Defining - dictionary scores = {'Name': ['Tom', 'Stacy', 'Kevin', 'Ron'], 'English': [78, 89, 91, 85], 'Science': [59, 68, 85, 65] } # Dictionary to -> DataFrame dataframe_1 = pd.DataFrame(scores) # Result Before Adding Column print(dataframe_1) # loc[] method - 'Maths' as column name dataframe_1.loc[:, "Maths"] = [99, 79, 45, 90] print(dataframe_1)
The Output will be -
![Use loc[]](https://codeparttime.com/wp-content/uploads/2022/10/09-Use-loc.png)
I hope you found this article helpful.

You may also like to Explore -
OrderedDict – Python Class | Why should we use it?
abs() function – Python – Find absolute of a number
startswith() Method – Python String – with Examples
Cheers!
Happy Coding.
About the Author
This article was authored by Rawnak.