Search code snippets, questions, articles...

Add a new column with data to Pandas Dataframe

If you have an existing Dataframe created using Pandas and want to add a new column with rows data, you can use the methods explained in this post.
import pandas as pd

my_data = {
  "names": ["Shivam", "John", "Prateek", "Gaurav"],
  "profession": ["Engineer", "Carpenter", "Writer", "marketer"]
}

df = pd.DataFrame(my_data)

# Add new column with rows data
df['address'] = ['India', 'New York', 'India', 'India']

print(df)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

Output

     names profession   address
0   Shivam   Engineer     India
1     John  Carpenter  New York
2  Prateek     Writer     India
3   Gaurav   marketer     India

Syntax

df['COLUMN_NAME'] = ['Value 1', 'Value 2', ...]

In the above code example, 

1. Import pandas library in your Python project file.

import pandas as pd

2. Created a Pandas Dataframe df with columns and rows.

3. Add a new column 'address' to this Dataframe df and print the new Dataframe.

df['address'] = ['India', 'New York', 'India', 'India']

Add new column using insert() function

We can use the insert() function of Pandas Dataframe to add a new column to an existing Dataframe. We can define position column names, values and allow duplicated while adding the new column with values.

Syntax

DataFrame.insert(location, column_name, values, allow_duplicates = False)

Code Example

import pandas as pd

my_data = {
  "names": ["Shivam", "John", "Prateek", "Gaurav"],
  "profession": ["Engineer", "Carpenter", "Writer", "marketer"]
}

df = pd.DataFrame(my_data)

# Add new column with rows data
df.insert(1, 'address', ['India', 'New York', 'India', 'India'], True)

print(df)

Output

     names   address profession
0   Shivam     India   Engineer
1     John  New York  Carpenter
2  Prateek     India     Writer
3   Gaurav     India   marketer

Added new column 'address' using DataFrame.insert() function at position 1(locations starts from 0).

import pandas as pd

my_data = {
  "names": ["Shivam", "John", "Prateek", "Gaurav"],
  "profession": ["Engineer", "Carpenter", "Writer", "marketer"]
}

# Create dataframe
df = pd.DataFrame(my_data)

# Add new column - department_id
df = df.assign(department_id = ['E01', 'C02', 'W03', 'M04'])

print(df)
The assign() function of DataFrame can also be used to insert a new column with rows to an existing DataFrame.
In the above code example, we are adding a column department_id to the Dataframe df using the assign() function.
Was this helpful?
0 Comments