JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas DataFrame.insert() Function

Author:JIYIK Last Updated:2025/05/01 Views:

Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame.


pandas.DataFrame.insert()Syntax

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

parameter

loc It is an integer parameter. It specifies the position of the new column. It must be greater than or equal to 0 and less than or equal to the number of columns.
column It is a string, integer, or an object. It is the label of the new column.
value It is an integer, sequence or array-like argument. It displays the values ​​of the new column.
allow_duplicates It is a Boolean parameter. It specifies whether two columns can be identical or not.

return

It modifies the original Dataframe and adds a new column.


Sample code: DataFrame.insert()Method to insert a column at the beginning

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe.insert(0, "Performance", ["Good", "Bad", "Better", "Better", "Best"])
print("The Modified Data frame is: \n")
print(dataframe)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Modified Data frame is: 

  Performance  Attendance    Name  Obtained Marks
0        Good          60  Olivia              90
1         Bad         100    John              75
2      Better          80   Laura              82
3      Better          78     Ben              64
4        Best          95   Kevin              45

The function adds a new column at the beginning.


Sample code: DataFrame.insert()Method to insert a column at the end

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe.insert(3, "Performance", ["Good", "Bad", "Better", "Better", "Best"])
print("The Modified Data frame is: \n")
print(dataframe)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Modified Data frame is: 

   Attendance    Name  Obtained Marks Performance
0          60  Olivia              90        Good
1         100    John              75         Bad
2          80   Laura              82      Better
3          78     Ben              64      Better
4          95   Kevin              45        Best

The function adds a new column at the end.


Sample code: DataFrame.insert()Method to insert duplicate columns

import pandas as pd

dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 78,4: 95},
                        'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                        'Obtained Marks': {0: 90, 1: 75, 2: 82, 3: 64, 4: 45}})
print("The Original Data frame is: \n")
print(dataframe)

dataframe.insert(0, "Attendance", [60, 100, 80, 78, 95], allow_duplicates= True)
print("The Modified Data frame is: \n")
print(dataframe)

Output:

The Original Data frame is: 

   Attendance    Name  Obtained Marks
0          60  Olivia              90
1         100    John              75
2          80   Laura              82
3          78     Ben              64
4          95   Kevin              45
The Modified Data frame is: 

   Attendance  Attendance    Name  Obtained Marks
0          60          60  Olivia              90
1         100         100    John              75
2          80          80   Laura              82
3          78          78     Ben              64
4          95          95   Kevin              45

Now the DataFrame has two columns with Attendancelabels .

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.idxmax() Function

Publish Date:2025/05/01 Views:79 Category:Python

Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.

Pandas DataFrame sort_index() Function

Publish Date:2025/05/01 Views:183 Category:Python

This tutorial explains how to use pandas.DataFrame.sort_index() the sort method to sort a Pandas DataFrame based on its index. We will use the DataFrame shown in the above example to explain how to sort a Pandas DataFrame based on the index

Pandas cut function

Publish Date:2025/05/01 Views:165 Category:Python

pandas.cut() The function can distribute the given data into a range, which can also be called bins . We will use the following DataFrame in this article. import pandas as pd df = pd . DataFrame( { "Name" : [ "Anish" , "Birat" , "Chirag" ,

Appending to an Empty DataFrame in Pandas

Publish Date:2025/05/01 Views:54 Category:Python

As we learned earlier, Pandas in Python is an open source module that we can use for data analysis and making machine learning models. It is Numpy used along with another package called as they go hand in hand to support multidimensional ar

Pandas DataFrame DataFrame.query() function

Publish Date:2025/04/30 Views:108 Category:Python

The pandas.DataFrame.query() method filters the rows of the caller DataFrame using the given query expression. pandas.DataFrame.query() grammar DataFrame . query(expr, inplace = False , ** kwargs) parameter expr Filter rows based on query e

Pandas DataFrame DataFrame.min() function

Publish Date:2025/04/30 Views:162 Category:Python

Python Pandas DataFrame.min() function gets the minimum value of the DataFrame object along the specified axis. pandas.DataFrame.min() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = None , ** kwargs) pa

Pandas DataFrame DataFrame.mean() function

Publish Date:2025/04/30 Views:86 Category:Python

Python Pandas DataFrame.mean() function calculates the mean of the values ​​of the DataFrame object over the specified axis. pandas.DataFrame.mean() grammar DataFrame . mean(axis = None , skipna = None , level = None , numeric_only = No

Pandas DataFrame DataFrame.isin() function

Publish Date:2025/04/30 Views:133 Category:Python

The pandas.DataFrame.isin(values) function checks whether each element in the caller DataFrame contains values the value specified in the input . pandas.DataFrame.isin(values) grammar DataFrame . isin(values) parameter values iterable - lis

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial