Of course! Labeling columns in Python is a fundamental task, especially when working with data. The method you use depends heavily on the data structure you're working with.

Here’s a comprehensive guide covering the most common scenarios, primarily using Pandas, which is the standard for data manipulation in Python.
The Short Answer (Pandas)
The most common way to label columns in Pandas is by directly assigning a list of strings to the DataFrame's .columns attribute.
import pandas as pd
# 1. Create a DataFrame without column labels (or with default ones)
data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
print("\nOriginal columns:", df.columns)
# 2. Label (rename) the columns by assigning a new list
# The new list must have the same number of elements as the original number of columns
new_labels = ['col_x', 'col_y', 'col_z']
df.columns = new_labels
print("\nLabeled DataFrame:")
print(df)
print("\nNew columns:", df.columns)
Output:
Original DataFrame:
A B C
0 1 4 7
1 2 5 8
2 3 6 9
Original columns: Index(['A', 'B', 'C'], dtype='object')
Labeled DataFrame:
col_x col_y col_z
0 1 4 7
1 2 5 8
2 3 6 9
New columns: Index(['col_x', 'col_y', 'col_z'], dtype='object')
Detailed Guide with Different Methods and Scenarios
Here are the most common ways to label or rename columns in Pandas, tailored to different needs.

Method 1: Assigning a New List of Labels (Simple & Direct)
This is the best method when you want to rename all columns at once.
import pandas as pd
df = pd.DataFrame({
'first_name': ['John', 'Jane', 'Dave'],
'last_name': ['Doe', 'Doe', 'Smith'],
'age_years': [28, 24, 35]
})
# Rename all columns
df.columns = ['First Name', 'Last Name', 'Age']
print(df)
Output:
First Name Last Name Age
0 John Doe 28
1 Jane Doe 24
2 Dave Smith 35
Important Rule: The new list of labels must have the exact same length as the number of columns in the DataFrame.
Method 2: Using the .rename() Method (Flexible & Powerful)
The .rename() method is more flexible because it allows you to rename specific columns without affecting the others.

You can use it in two main ways:
A) Using a Dictionary (Most Common)
Specify the old name as the key and the new name as the value.
import pandas as pd
df = pd.DataFrame({
'first_name': ['John', 'Jane'],
'last_name': ['Doe', 'Doe'],
'age_years': [28, 24]
})
# Rename only specific columns
df_renamed = df.rename(columns={
'first_name': 'First Name',
'age_years': 'Age'
})
print(df_renamed)
Output:
First Name Last Name Age
0 John Doe 28
1 Jane Doe 24
Notice that last_name was not changed. The original df also remains unchanged.
B) Using a Function
You can apply a function to all column names. This is very useful for consistent formatting, like making everything lowercase or replacing spaces.
# Example: Convert all column names to lowercase and replace spaces with underscores
df = pd.DataFrame({
'First Name': ['John', 'Jane'],
'Last Name': ['Doe', 'Doe'],
'Age': [28, 24]
})
# Define a lambda function to transform the column names
df.columns = df.columns.str.lower().str.replace(' ', '_')
print(df)
Output:
first_name last_name age
0 john doe 28
1 jane doe 24
The .rename() method can also take a function:
df.rename(columns=lambda x: x.lower().replace(' ', '_'), inplace=True)
Note:
inplace=Truemodifies the DataFrame directly. If you omit it (or set it toFalse), it returns a new DataFrame.
Method 3: Setting Column Labels from a File (CSV)
Often, your data file (like a CSV) doesn't have a header row. In this case, you can provide the column labels when you read the file using pd.read_csv().
# Sample data file 'data_no_header.csv' looks like this:
# John,Doe,28
# Jane,Doe,24
# Dave,Smith,35
# Define the column labels you want to use
column_labels = ['First Name', 'Last Name', 'Age']
# Read the CSV and specify the column names
df_labeled = pd.read_csv('data_no_header.csv', names=column_labels)
print(df_labeled)
Output:
First Name Last Name Age
0 John Doe 28
1 Jane Doe 24
2 Dave Smith 35
Other Data Structures
While Pandas is the most common, here's a quick look at how you'd handle labeling in other structures.
Python Lists of Lists
If your data is a simple list of lists, the "columns" are just the first row of your data structure.
# Data where the first list is the header
data = [
['Product', 'Price', 'In_Stock'],
['Apple', 1.0, True],
['Banana', 0.5, False]
]
# The labels are simply the first element
column_labels = data[0]
# The data is everything after the first element
rows = data[1:]
print("Column Labels:", column_labels)
print("Data Rows:", rows)
NumPy Arrays
NumPy arrays do not have a built-in column label system like Pandas DataFrames. They are designed for numerical operations. If you need labels, you should use a Pandas DataFrame.
import numpy as np
# Create a NumPy array
data_array = np.array([
[1, 2, 3],
[4, 5, 6]
])
# To add labels, you convert it to a Pandas DataFrame
df_from_array = pd.DataFrame(data_array, columns=['Col A', 'Col B', 'Col C'])
print(df_from_array)
Summary: Which Method to Use?
| Scenario | Recommended Method |
|---|---|
| Renaming all columns at once. | df.columns = ['new1', 'new2', ...] |
| Renaming only a few specific columns. | df.rename(columns={'old': 'new'}) |
| Renaming columns based on a pattern (e.g., lowercase, replace spaces). | df.columns = df.columns.str.lower().str.replace(...) |
| Reading data from a file without a header. | pd.read_csv(..., names=['col1', 'col2']) |
| You need to preserve the original DataFrame while creating a new one. | df_renamed = df.rename(...) (without inplace=True) |
