Using pandas.DataFrame.items() method

Updated: February 19, 2024 By: Guest Contributor Post a comment


In this tutorial, we delve into an important yet often overlooked method for handling dataframes in the pandas library: the .items() method. Pandas is a cornerstone tool for data manipulation and analysis in Python, offering robust functions to reshape, pivot, merge, and slice datasets. Specifically, DataFrame.items() provides a powerful gateway to iterate over column labels and content, enhancing the flexibility and efficiency of data operations.

Firstly, ensure you have pandas installed in your python environment:

pip install pandas

Understanding pandas.DataFrame.items() by Examples

The DataFrame.items() method allows developers to iterate through (column label, series) pairs in a DataFrame. This can be incredibly insightful when analyzing columns or applying transformations at a column level.

Basic Usage

Let’s start with a simple demonstration:

import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]

for label, content in df.items():

The output from this snippet would individually list the contents of each column:

0    1
1    2
2    3
Name: A, dtype: int64

0    4
1    5
2    6
Name: B, dtype: int64

0    7
1    8
2    9
Name: C, dtype: int64

Applying Functions

Moving on to a more practical application, the .items() method can be used to apply a function across all columns:

def custom_transform(series):
    return series * 10

for label, series in df.items():
    df[label] = custom_transform(series)


Output would reflect the transformed DataFrame:

   A   B   C
0  10  40  70
1  20  50  80
2  30  60  90

Filtering Columns

In situations requiring conditional operations across certain columns, .items() facilitates an elegant solution:

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': ['One', 'Two', 'Three'],
    'C': [7, 8, 9]

for label, series in df.items():
    if series.dtype == 'int64':
        # Perform some operation

This snippet sums the integer columns, ignoring others:



Advanced Operations

For data scientists and engineers seeking deeper insights, iterating over columns with .items() method can uncover unique patterns. Consider an analysis to compute columnwise correlation:

correlation_matrix = df.corr()
for label, series in df.items():
    correlated_to = correlation_matrix[label].idxmax()
    print(f"{label} is most correlated to {correlated_to}.")

This approach ensures a focused analysis without the boilerplate code typically involved in such operations.


To summarize, the DataFrame.items() method from pandas offers a structured way to iteratively access and manipulate DataFrame columns. Whether it’s applying functions, conducting analytics, or refining data based on conditions, .items() furnishes developers with an intuitive pathway for columnar data examination. The potency and utility of this method underline its importance in the expansive toolkit that pandas offers to Python developers in the realm of data manipulation and analysis.