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.columnsHere'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.columnsLet’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)
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?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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4