How to Add Column to DataFrame in pandas: Simple Guide
To add a column to a pandas DataFrame, assign a new column name with values using
df['new_column'] = values. The values can be a list, a scalar, or a pandas Series matching the DataFrame's length.Syntax
Use df['new_column'] = values to add a new column to a DataFrame.
df: your existing DataFrame'new_column': the name of the new column as a stringvalues: data for the new column; can be a list, scalar (single value), or Series matching the DataFrame length
python
df['new_column'] = valuesExample
This example shows how to add a new column named Age to a DataFrame with a list of values.
python
import pandas as pd data = {'Name': ['Alice', 'Bob', 'Charlie']} df = pd.DataFrame(data) # Add new column 'Age' df['Age'] = [25, 30, 35] print(df)
Output
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
Common Pitfalls
Common mistakes when adding columns include:
- Assigning a list or Series with a different length than the DataFrame rows causes an error.
- Using a scalar value adds the same value to all rows, which may be unintended.
- For conditional columns, use
np.whereordf.locto avoid errors.
python
import pandas as pd import numpy as np data = {'Name': ['Alice', 'Bob', 'Charlie']} df = pd.DataFrame(data) # Wrong: list length mismatch # df['Age'] = [25, 30] # This will raise a ValueError # Right: scalar value (same age for all) df['Age'] = 28 # Right: conditional column df['Is_Adult'] = np.where(df['Age'] >= 18, True, False) print(df)
Output
Name Age Is_Adult
0 Alice 28 True
1 Bob 28 True
2 Charlie 28 True
Quick Reference
Summary tips for adding columns:
| Action | Syntax | Notes |
|---|---|---|
| Add column with list | df['col'] = [val1, val2, ...] | List length must match DataFrame rows |
| Add column with scalar | df['col'] = value | Same value for all rows |
| Add column with Series | df['col'] = pd.Series([...]) | Series index aligns with DataFrame |
| Add conditional column | df['col'] = np.where(condition, val1, val2) | Use numpy for conditions |
Key Takeaways
Use df['new_column'] = values to add a column to a pandas DataFrame.
Ensure the values length matches DataFrame rows or use a scalar for the same value.
Common errors come from mismatched lengths or unintended scalar assignments.
Use numpy or df.loc for adding columns based on conditions.
Adding columns is a simple and powerful way to enrich your DataFrame.