Pandas DataFrame.columns


Pandas DataFrame.columns

The DataFrame.columns property in pandas is used to access or modify the column labels of a DataFrame. The column labels help identify and work with data in a structured way.


Syntax

The syntax to access or modify the columns of a DataFrame is:

DataFrame.columns

Here, DataFrame refers to the pandas DataFrame whose column labels are being accessed or modified.


Examples

Accessing Column Labels

You can access the column labels of a DataFrame using the DataFrame.columns property.

Python Program

import pandas as pd

data = {
    'Name': ['Arjun', 'Ram', 'Krishna'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

# Accessing column labels
print("Columns:", df.columns)

Output

Columns: Index(['Name', 'Age', 'City'], dtype='object')

Renaming Columns

To rename the columns of a DataFrame, you can assign a new list of column labels to DataFrame.columns.

Python Program

import pandas as pd

data = {
    'Name': ['Arjun', 'Ram', 'Krishna'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

# Renaming columns
df.columns = ['Full Name', 'Age (Years)', 'City of Residence']

# Display updated DataFrame
print("DataFrame with Renamed Columns:")
print(df)

# Accessing updated column labels
print("Updated Columns:", df.columns)

Output

DataFrame with Renamed Columns:
     Full Name  Age (Years)    City of Residence
0       Arjun           25           New York
1         Ram           30       Los Angeles
2     Krishna           35           Chicago

Updated Columns: Index(['Full Name', 'Age (Years)', 'City of Residence'], dtype='object')

Setting Columns Programmatically

You can generate and assign column names programmatically using Python code.

Python Program

import pandas as pd

data = {
    'Name': ['Arjun', 'Ram', 'Krishna'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

# Setting column names programmatically
df.columns = [f'Column_{i}' for i in range(1, len(df.columns) + 1)]

# Display updated DataFrame
print("DataFrame with Programmatic Column Names:")
print(df)

# Accessing updated column labels
print("Updated Columns:", df.columns)

Output

DataFrame with Programmatic Column Names:
  Column_1  Column_2       Column_3
0    Arjun        25       New York
1      Ram        30   Los Angeles
2  Krishna        35       Chicago

Updated Columns: Index(['Column_1', 'Column_2', 'Column_3'], dtype='object')

Accessing Columns' Attributes

The DataFrame.columns property allows you to inspect attributes such as values and type of column labels.

Python Program

import pandas as pd

data = {
    'Name': ['Arjun', 'Ram', 'Krishna'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)

# Accessing column values and type
print("Column Values:", df.columns.values)
print("Column Type:", type(df.columns))

Output

Column Values: ['Name' 'Age' 'City']
Column Type: 

Summary

In this tutorial, we focused on the DataFrame.columns property in pandas. We learned how to access column labels, rename them, set them programmatically, and inspect their attributes. Understanding how to work with column labels is essential for effective data manipulation in pandas.


Python Libraries