Open In App

Python | Pandas dataframe.insert()

Improve
Improve
Like Article
Like
Save
Share
Report

Pandas insert method allows the user to insert a column in a data frame or series(1-D Data frame). A column can also be inserted manually in a data frame by the following method, but there isn’t much freedom here. 
For example, even column location can’t be decided and hence the inserted column is always inserted in the last position in Python.

Pandas DataFrame.insert() Syntax

Syntax: 

DataFrameName.insert(loc, column, value, allow_duplicates = False)

Parameters: 
loc: loc is an integer which is the location of column where we want to insert new column. This will shift the existing column at that position to the right. 
column: column is a string which is name of column to be inserted. 
value: value is simply the value to be inserted. It can be int, string, float or anything or even series / List of values. Providing only one value will set the same value for all rows. 
allow_duplicates : allow_duplicates is a boolean value which checks if column with same name already exists or not.  

What is dataframe.insert() in Pandas ?

`DataFrame.insert()` in Pandas is a method used to insert a new column or columns at a specified location in a DataFrame. It allows users to add columns with specified names and values, providing flexibility in DataFrame customization. The method takes parameters such as column index, column name, and column values, enabling precise control over the DataFrame’s structure. This function is useful for managing and organizing data within Pandas DataFrames efficiently.

dataframe.insert() Examples

In Pandas, you may use DataFrame to extend or add a new column to an already existing DataFrame. The insert() method adds a new column to the DataFrame that already exists. Another technique for adding a new column is DataFrame.assign(); however, this method returns a new Dataframe after adding a new column. there are various uses of dataframe.insert() here we explain some generally used methods those are following.

  1. Add an Extra Column with Static Value
  2. Insert New Multiple Columns into the DataFrame
  3. Use DataFrame.insert() with pandas.series() Function
  4. Insert a Calculated Column
  5. Insert a Column with Conditional Values
  6. Insert a Column with Random Values

Create a Dataframe

In this example code uses the pandas library to create a DataFrame named `df` from a dictionary `data` containing information about individuals’ age, name, and address, and then prints the original DataFrame.

Python3




import pandas as pd
 
# Create a sample DataFrame
data = {'age': [21, 22, 27, 28],
        'name': ['Pratham', 'Shivang', 'Suraj', 'Satyam'],
        'address': ['MP', 'Delhi', 'UP','Noida']}
 
df = pd.DataFrame(data)
 
# Display the original DataFrame
print("Original DataFrame:")
print(df)


Output :

Original DataFrame:
   age     name address
0   21  Pratham      MP
1   22  Shivang   Delhi
2   27    Suraj      UP
3   28   Satyam   Noida

Time Complexity: O(1)
Space Complexity: O(1)

Add Column with Static Value to Pandas Dataframe

The `dataframe.insert()` method adds a new column to a DataFrame. It takes parameters for the column index, column name, and a static value, allowing the insertion of a column with a fixed value across all rows.

Example : In this example code adds a new column named ‘occupation’ to a DataFrame (df) with the static value ‘Male’ for all rows and then prints the updated DataFrame.

Python3




# Add Extra Column with Static Value
static_value = 'Male'
df['occupation'] = static_value
print(df)


Output :

 age     name address occupation
0   21  Pratham      MP       Male
1   22  Shivang   Delhi       Male
2   27    Suraj      UP       Male
3   28   Satyam   Noida       Male

Time Complexity: O(1)
Space Complexity: O(n)

Pandas Add Multiple Columns to DataFrame

The `dataframe.insert()` method is used to add multiple new columns to a DataFrame in pandas. It takes parameters for the insertion position, column names, and values. This enables efficient insertion of columns at specified locations within the DataFrame.

Example : In this example code creates a DataFrame (`new_columns_df`) with additional ‘height’ and ‘weight’ columns and then combines it with the original DataFrame (`df`) along columns, resulting in `result_df` displaying the combined data.

Python3




# Insert New Multiple Columns into the DataFrame
new_columns_data = {'height': [160, 175, 168, 0],
                    'weight': [55, 70, 65, 0]} 
 
new_columns_df = pd.DataFrame(new_columns_data)
df = pd.DataFrame(data)
 
# Combine the original DataFrame and the new columns DataFrame
result_df = pd.concat([df, new_columns_df], axis=1)
print(result_df)


Output :

 age     name address  height  weight
0   21  Pratham      MP     160      55
1   22  Shivang   Delhi     175      70
2   27    Suraj      UP     168      65
3   28   Satyam   Noida       0       0

Time Complexity: O(n+m)
Space Complexity: O(n+m)

Add column to Pandas using DataFrame.insert() & pandas.series() Function

The method uses the `insert()` function in pandas DataFrame to add a new column. It employs the `pandas.Series()` function to create a new column as a Series. The `dataframe.insert()` method inserts this new column at a specified position in the DataFrame.

Example : In this example code creates a Pandas DataFrame ‘df’ with initial data, then inserts ‘height’ and ‘weight’ columns from another DataFrame ‘new_columns_df’ at positions 3 and 4, and finally prints the resulting DataFrame.

Python3




# Create a DataFrame with 'height' and 'weight' columns
new_columns_data = {'height': [170, 175, 180, 165],
                    'weight': [65, 70, 75, 60]}
new_columns_df = pd.DataFrame(new_columns_data)
 
# Create the main DataFrame
df = pd.DataFrame(data)
 
# Use DataFrame.insert() & pandas.Series() Function
df.insert(3, 'height', new_columns_df['height'])
df.insert(4, 'weight', pd.Series(new_columns_df['weight']))
 
# Display the final DataFrame
print(df)


Output :

 age     name address  height  weight
0   21  Pratham      MP     170      65
1   22  Shivang   Delhi     175      70
2   27    Suraj      UP     180      75
3   28   Satyam   Noida     165      60

Time Complexity: O(n)
Space Complexity: O(n)

Insert Calculated Column in Pandas using dataframe.insert()

The method `dataframe.insert()` is used to add a new calculated column to a DataFrame in Python. It allows specifying the column index, column name, and the calculation expression. This method efficiently inserts the new column at the specified position without the need to create an entirely new DataFrame.

Example : In this example code adds a new column named ‘birth_year’ to a DataFrame (‘df’), calculated by subtracting the values in the ‘age’ column from 2023, and then prints the updated DataFrame.

Python3




# Insert a calculated column 'birth_year' based on the 'age' column
df.insert(1, 'birth_year', 2023 - df['age'])
 
# Display the DataFrame after inserting the calculated column
print("\nDataFrame after inserting a calculated column:")
print(df)


Output:

DataFrame after inserting a calculated column:
   age  birth_year     name address  height  weight
0   21        2002  Pratham      MP     170      65
1   22        2001  Shivang   Delhi     175      70
2   27        1996    Suraj      UP     180      75
3   28        1995   Satyam   Noida     165      60

Time Complexity: O(n)
Space Complexity: O(1)

Insert a Column with Conditional Values using dataframe.insert()

The `dataframe.insert()` method is used to add a new column to a DataFrame in Python. It allows specifying the column’s position, name, and values. Conditional values can be assigned to the new column based on a specified condition, providing a convenient way to add data with a conditional logic in a single line of code.

Example : In this example code adds a new column named ‘status’ to a DataFrame (‘df’) at position 3, assigning ‘Adult’ if the corresponding ‘age’ is 18 or older, and ‘Minor’ otherwise. It then prints the DataFrame with the newly inserted ‘status’ column.

Python3




# Insert a new column 'status' based on a condition
df.insert(3, 'status', ['Adult' if age >= 18 else 'Minor' for age in df['age']])
 
# Display the DataFrame after inserting the column with conditional values
print("\nDataFrame after inserting a column with conditional values:")
print(df)


Output :

DataFrame after inserting a column with conditional values:
   age  birth_year     name status address  height  weight
0   21        2002  Pratham  Adult      MP     170      65
1   22        2001  Shivang  Adult   Delhi     175      70
2   27        1996    Suraj  Adult      UP     180      75
3   28        1995   Satyam  Adult   Noida     165      60

Time Complexity: O(n)
Space Complexity: O(1)

Insert a Column with Random Values using dataframe.insert()

The `dataframe.insert()` method in pandas is used to insert a new column into a DataFrame. By specifying the column index, name, and values using the `column` parameter, it efficiently adds a new column with random values. This provides a convenient way to enhance or modify a DataFrame .

Example : In this example code uses NumPy to add a new column named ‘random_value’ to a DataFrame (`df`) at index 2, filling it with random numeric values. The final DataFrame is then printed to display the result.

Python3




import numpy as np
 
# Insert a new column 'random_value' with random numeric values
df.insert(2, 'random_value', np.random.rand(len(df)))
 
# Display the DataFrame after inserting the column with random values
print("\nDataFrame after inserting a column with random values:")
print(df)


Output :

DataFrame after inserting a column with random values:
   age     name  random_value address
0   21  Pratham      0.636732      MP
1   22  Shivang      0.676477   Delhi
2   27    Suraj      0.513436      UP
3   28   Satyam      0.900731   Noida

Time Complexity: O(n)
Space Complexity: O(m*n)



Last Updated : 15 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads