Pandas DataFrame.columns (original) (raw)
Last Updated : 11 Jul, 2025
In Pandas, **DataFrame.columns attribute returns the column names of a DataFrame. It gives access to the column labels, returning an Index object with the column labels that may be used for viewing, modifying, or creating new column labels for a DataFrame.
**Note: _This attribute doesn't require any parameters and simply returns the column labels of the DataFrame when called.
Retrieve Column Labels Using DataFrame.columns
Here's an example showing how to use **DataFrame.columns in order to obtain column labels from a DataFrame.
Python `
import pandas as pd
df = pd.DataFrame({ 'Weight': [45, 88, 56, 15, 71], 'Name': ['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'], 'Age': [14, 25, 55, 8, 21] })
index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5'] df.index = index_
print("DataFrame:") display(df)
result = df.columns
print("\n Name of Columns of Pandas DataFrame") print(result)
`
**Output:
Column Labels retrieved Using DataFrame.columns
Let’s take a look at a second example where the DataFrame contains missing values (NaN). To retrieve the column labels from this DataFrame, we use the DataFrame.columns attribute:
Python `
import pandas as pd
df = pd.DataFrame({ "A": [12, 4, 5, None, 1], "B": [7, 2, 54, 3, None], "C": [20, 16, 11, 3, 8], "D": [14, 3, None, 2, 6] })
idx = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5'] df.index = idx
res = df.columns
print("\n Name of Columns of Pandas DataFrame") print(res)
`
Output
Name of Columns of Pandas DataFrame Index(['A', 'B', 'C', 'D'], dtype='object')
As shown, the **DataFrame.columns attribute returns the column labels even when the data contains missing values.
Why Use DataFrame.columns?
- **Access Column Names: Easily access the column labels of a DataFrame for further analysis.
- **Manipulate Columns: You can modify the DataFrame.columns attribute to rename columns as needed.
- **Inspect Data: Quickly inspect and confirm the column names to ensure accurate data processing.
The **DataFrame.columns attribute in Pandas is an essential tool for managing and working with DataFrame column labels. By using this attribute, users can work efficiently with Pandas DataFrames, whether for data cleaning, transformation, or analysis tasks.
**Related Article: