0
0
PandasHow-ToBeginner · 3 min read

How to Insert a Column at a Specific Position in pandas DataFrame

Use the DataFrame.insert() method to add a new column at a specific position in a pandas DataFrame. Specify the position index, column name, and values as arguments to insert().
📐

Syntax

The DataFrame.insert() method adds a new column at a specified position.

  • loc: The integer index where the new column will be inserted (0-based).
  • column: The name of the new column as a string.
  • value: The data for the new column (list, Series, or scalar).
  • allow_duplicates (optional): Whether to allow duplicate column names (default is False).
python
DataFrame.insert(loc, column, value, allow_duplicates=False)
💻

Example

This example shows how to insert a new column named 'Age' at position 1 in a DataFrame.

python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'City': ['NY', 'LA', 'Chicago']}
df = pd.DataFrame(data)

# Insert 'Age' column at position 1
ages = [25, 30, 35]
df.insert(1, 'Age', ages)

print(df)
Output
Name Age City 0 Alice 25 NY 1 Bob 30 LA 2 Charlie 35 Chicago
⚠️

Common Pitfalls

Common mistakes when inserting columns include:

  • Using df['new_col'] = values which always adds the column at the end, not at a specific position.
  • Specifying a loc index outside the valid range (less than 0 or greater than number of columns).
  • Trying to insert a column with a name that already exists without setting allow_duplicates=True.
python
import pandas as pd

data = {'A': [1, 2], 'B': [3, 4]}
df = pd.DataFrame(data)

# Wrong: adds column at the end
# df['C'] = [5, 6]

# Right: insert at position 1
# df.insert(1, 'C', [5, 6])

# Wrong: loc out of range
# df.insert(5, 'D', [7, 8])  # Raises IndexError

# Right: loc within range
# df.insert(2, 'D', [7, 8])
📊

Quick Reference

Remember these tips when inserting columns:

  • Use insert() to place columns anywhere.
  • loc is zero-based index for column position.
  • Default allow_duplicates=False prevents duplicate column names.
  • Use df['col'] = val only to add columns at the end.

Key Takeaways

Use DataFrame.insert(loc, column, value) to add a column at a specific position.
The loc parameter is zero-based and must be within the current column range.
Avoid duplicate column names unless allow_duplicates=True is set.
Assigning a column with df['col'] = val always adds it at the end.
Check your column positions carefully to prevent IndexError.