0
0
PandasHow-ToBeginner · 3 min read

How to Use Columns in pandas: Syntax and Examples

In pandas, you use DataFrame['column_name'] to access or create columns. You can assign values to columns to modify or add new ones, and use del DataFrame['column_name'] to remove columns.
📐

Syntax

To work with columns in pandas, use the following syntax:

  • df['column_name']: Access or create a column named column_name.
  • df['new_column'] = values: Add or modify a column with new values.
  • del df['column_name']: Delete a column from the DataFrame.
python
df['column_name']  # Access or create a column

df['new_column'] = values  # Add or modify a column

del df['column_name']  # Delete a column
💻

Example

This example shows how to access, add, modify, and delete columns in a pandas DataFrame.

python
import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35]
})

# Access the 'Age' column
ages = df['Age']

# Add a new column 'City'
df['City'] = ['NY', 'LA', 'Chicago']

# Modify the 'Age' column by adding 1 year
df['Age'] = df['Age'] + 1

# Delete the 'City' column
del df['City']

ages, df
Output
(0 25 1 30 2 35 Name: Age, dtype: int64, Name Age 0 Alice 26 1 Bob 31 2 Charlie 36)
⚠️

Common Pitfalls

Common mistakes when using columns in pandas include:

  • Using dot notation like df.column_name which can fail if the column name has spaces or conflicts with DataFrame methods.
  • Trying to access a non-existing column without checking, which raises a KeyError.
  • Assigning a list of wrong length to a new column, causing a ValueError.
python
import pandas as pd

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

# Wrong: dot notation with space in column name
# df.New Column = [4, 5, 6]  # This will cause an error

# Right: use bracket notation

df['New Column'] = [4, 5, 6]

# Wrong: assigning list of wrong length
# df['B'] = [7, 8]  # ValueError

# Right: assign list with correct length

df['B'] = [7, 8, 9]

df
Output
A New Column B 0 1 4 7 1 2 5 8 2 3 6 9
📊

Quick Reference

OperationSyntaxDescription
Access columndf['column_name']Get a column as a Series
Add/modify columndf['new_col'] = valuesCreate or update a column
Delete columndel df['column_name']Remove a column from DataFrame
Check if column exists'column_name' in dfReturns True or False
Rename columnsdf.rename(columns={'old':'new'})Change column names

Key Takeaways

Use bracket notation df['column_name'] to safely access or create columns.
Assign lists or Series of correct length to add or modify columns.
Avoid dot notation for columns to prevent errors with special names.
Use del df['column_name'] to remove columns from your DataFrame.
Check if a column exists with 'column_name' in df before accessing.