add new column in dataframe

How to Add New Column to Existing DataFrame

Python pandas allows you to perform data analysis and processing using simple and powerful functions. Many times you may need to add column to existing dataframe. In this article, we will learn how to add new column to existing DataFrame. There are several ways to do this in Python Pandas.


How to Add New Column to Existing DataFrame

Here are the different ways to add new column to existing dataframe.


1. Using Lists

You can also add a new column by declaring a list as new column in your dataframe. Here is an example where we have created dataframe and declared a list as new column. Please note, the number of elements in list should be equal to the number of rows in your dataframe, else you will get an error.

# Import pandas package
import pandas as pd

# Define a dictionary containing Students data
data = {'Name': ['Jay', 'Prince', 'Gary', 'AJ'],
		'Height': [5.1, 6.2, 5.1, 5.2],
		'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data)

# Declare a list that is to be converted into a column
address = ['Denver', 'Baltimore', 'San Francisco', 'NYC']

# Using 'Address' as the column name
# and equating it to the list
df['Address'] = address

# Observe the result
df

In the above example, the new column is added after all the existing columns in dataframe.


2. Using Dataframe.insert()

Every dataframe has a built in function insert() that allows you to add new column to dataframe. It allows you to add new column at any position, not just at the end. Here is an example to add new column at position 2, between columns Height & Qualification.

# Import pandas package
import pandas as pd

# Define a dictionary containing Students data
data = {'Name': ['Jay', 'Prince', 'Gary', 'AJ'],
		'Height': [5.1, 6.2, 5.1, 5.2],
		'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data)

# Using DataFrame.insert() to add a column
df.insert(2, "Age", [21, 23, 24, 21], True)

# Observe the result
df

Insert function also provides many options to add new data to your dataframe. In the above code, we provide 4 arguments for insert() function – position of new column, name of new column, list to be used for new column, allow duplicate values.


3. Using Dataframe.assign()

You can also use dataframe.assign() function to add new column as shown below. In the following example, we add new column address using list of values. This new column is added at the end of dataframe.

# Import pandas package
import pandas as pd

# Define a dictionary containing Students data
data = {'Name': ['Jay', 'Prince', 'Gary', 'AJ'],
		'Height': [5.1, 6.2, 5.1, 5.2],
		'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}


# Convert the dictionary into DataFrame
df = pd.DataFrame(data)

# Using 'Address' as the column name and equating it to the list
df2 = df.assign(address = ['Denver', 'Baltimore', 'San Francisco', 'NYC'])

# Observe the result
df2


4. Using Dictionary

You can also create a new column using dictionary. The key value is used as column name and the dictionary’s values are used as column values. In the following example, we create a dataframe with 3 columns. We create new column called ‘address’ and pass the dictionary variable address to be used for the new column’s values.

# Import pandas package
import pandas as pd

# Define a dictionary containing Students data
data = {'Name': ['Jay', 'Prince', 'Gary', 'AJ'],
		'Height': [5.1, 6.2, 5.1, 5.2],
		'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Define a dictionary with key values of
# an existing column and their respective
# value pairs as the # values for our new column.
address = {'Jay': 'Denver', 'Prince': 'Baltimore',
		'Gary': 'Sand Francisco', 'AJ': 'NYC'}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data)

# Provide 'Address' as the column name
df['Address'] = address

# Observe the output
df

In the above code, the dictionary address key-value pairs where each key is an existing value of the dataframe, and the value is the corresponding value for the new column.

In this article, we have learnt several different ways to add new column in Pandas Dataframe.

Also read:

How to Change Element Class Property Using JavaScript
How to Get Value of Data Attribute in JavaScript
How to Shuffle Array in JavaScript
How to Remove Local Timezone from Date in JavaScript
How to Format Date to MMDDYYYY in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *