Insert Column into DataFrame

To insert column into DataFrame at specified location, call insert() method on this DataFrame and pass the required details like location of insertion, details of column to insert, etc.

In this tutorial, we will learn the syntax of DataFrame.insert() method and how to use this method to insert a column into DataFrame.

Syntax

The syntax of DataFrame.insert() method is

</>
Copy
DataFrame.insert(loc, column, value, allow_duplicates=False)

where

ParameterValueDescription
locint.Insertion index. Must verify 0 <= loc <= len(columns).
columnstr, number, or hashable object.Label of the inserted column.
valueint, Series, or array-like.
allow_duplicatesbool.
Default value is False.

Return Value

None.

Examples

Insert Column into DataFrame at location=1

In the following program, we take a DataFrame with columns 'name' and 'quant'. We insert a new column with name 'loc' into this DataFrame at location = 1 with column values ['Abc', 'Xyz', 'Mno'].

Example.py

</>
Copy
import pandas as pd

data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)

print('Original DataFrame')
print(df)

df.insert(1, 'place', ['Abc', 'Xyz', 'Mno'])
print('\nAfter inserting column')
print(df)

Output

Original DataFrame
     name  quant
0   apple     40
1  banana     50
2  cherry     60

After inserting column
     name place  quant
0   apple   Abc     40
1  banana   Xyz     50
2  cherry   Mno     60

Column Length does not match the length of DataFrame’s index

Length of new column that we insert must match the length of DataFrame’s index. If length of new column does not match the length of index, then ValueError is raised.

Example.py

</>
Copy
import pandas as pd

data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)

print('Original DataFrame')
print(df)

df.insert(1, 'place', ['Abc', 'Xyz'])
print('\nAfter inserting column')
print(df)

Output

ValueError: Length of values (2) does not match length of index (3)

Column Already Exists

If a column already exists with the same column name as that of the new column that we would like to insert, ValueError is raised unless we pass True for allow_duplicates parameter.

In the following program, we take a DataFrame with columns 'name' and 'quant'. We insert a new column with name 'quant' into this DataFrame. Since a column with this name already exists, Value Error is raised.

Example.py

</>
Copy
import pandas as pd

data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)

print('Original DataFrame')
print(df)

df.insert(1, 'name', ['Abc', 'Xyz', 'Mno'])
print('\nAfter inserting column')
print(df)

Output

ValueError: cannot insert name, already exists

Let us pass True for allow_duplicates parameter.

Example.py

</>
Copy
import pandas as pd

data = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data)

print('Original DataFrame')
print(df)

df.insert(1, 'name', ['Abc', 'Xyz', 'Mno'], allow_duplicates=True)
print('\nAfter inserting column')
print(df)

Output

Original DataFrame
     name  quant
0   apple     40
1  banana     50
2  cherry     60

After inserting column
     name name  quant
0   apple  Abc     40
1  banana  Xyz     50
2  cherry  Mno     60

Conclusion

In this Pandas Tutorial, we learned how to insert a new column into DataFrame using pandas DataFrame.insert() method.