The insert()
function in Pandas is used to insert a column into a DataFrame at a specified location.
Example
import pandas as pd
# sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6]}
df = pd.DataFrame(data)
# insert a new column C at index 1
df.insert(1, 'C', [7, 8, 9])
print(df)
'''
Output
A C B
0 1 7 4
1 2 8 5
2 3 9 6
'''
insert() Syntax
The syntax of the insert()
method in Pandas is:
DataFrame.insert(loc, column, value, allow_duplicates=False)
insert() Arguments
The insert()
method in Pandas has the following arguments:
loc
: the integer index of the column before which the new column is to be insertedcolumn
: the name to assign to the new columnvalue
: the data to insertallow_duplicates
(optional): whether to allow columns with the same name.
insert() Return Value
The insert()
method does not return any value. It modifies the DataFrame in-place.
Example 1: Insert a Scalar Value as a Column
import pandas as pd
data = {'A': [1, 2, 3],
'B': [4, 5, 6]}
df = pd.DataFrame(data)
# insert a scalar value at index 0
df.insert(0, 'C', 10)
print(df)
Output
C A B 0 10 1 4 1 10 2 5 2 10 3 6
In this example, we added a new column 'C'
to df at index 0 with a constant value of 10.
Example 2: Insert a Column with Duplicate Name
import pandas as pd
data = {'A': [1, 2, 3],
'B': [4, 5, 6]}
df = pd.DataFrame(data)
# insert a column that already exists
# at index 2
df.insert(2, 'B', [7, 8, 9], allow_duplicates=True)
print(df)
Output
A B B 0 1 4 7 1 2 5 8 2 3 6 9
In this example, we set the allow_duplicates
flag to True
, which allows a new column with the already existing name 'B'
to be inserted at specified index without raising an error.