Open In App

Python | Pandas dataframe.insert()

Last Updated : 15 Dec, 2023
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)



Previous Article
Next Article

Similar Reads

Bulk Insert to Pandas DataFrame Using SQLAlchemy - Python
Let's start with SQLAlchemy, a Python library that allows communication with databases(MySQL, PostgreSQL etc.) and Python. This library is used as an Object Relational Mapper tool that translates Python classes to tables in relational databases and automatically converts function calls to SQL statements that gives application developers the full po
3 min read
Insert row at given position in Pandas Dataframe
Inserting a row in Pandas DataFrame is a very straight forward process and we have already discussed approaches in how insert rows at the start of the Dataframe. Now, let's discuss the ways in which we can insert a row at any position in the dataframe having integer based index.Solution #1 : There does not exist any in-built function in pandas whic
3 min read
How to insert a pandas DataFrame to an existing PostgreSQL table?
In this article, we are going to see how to insert a pandas DataFrame to an existing PostgreSQL table. Modules neededpandas: Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fas
3 min read
Insert a given column at a specific position in a Pandas DataFrame
In this comprehensive guide, we will leverage the powerful DataFrame.insert() the method provided by the Pandas library to effectively Insert a given column at a specific position in a Pandas Dataframe. Create a Sample DataFrame In this example below code uses Pandas to create a DataFrame named 'df' from a dictionary with columns 'col2' and 'col3'
4 min read
Python | Pandas DataFrame.fillna() to replace Null values in dataframe
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like the pandas dropna() method manages and rem
3 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?
We might sometimes need a tidy/long-form of data for data analysis. So, in python's library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index
4 min read
Replace values of a DataFrame with the value of another DataFrame in Pandas
In this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string, list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame method are get replaced with another value dynamically.
4 min read
Article Tags :
Practice Tags :