Pandas DataFrame Items Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the items() method in the Pandas library to iterate over a DataFrame's columns. This method returns a generator object that contains tuples of column names and their corresponding Series.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/DataSelectionGroup -.-> pandas/conditional_selection("`Conditional Selection`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} pandas/select_columns -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} pandas/conditional_selection -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/for_loops -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/lists -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/tuples -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/dictionaries -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/importing_modules -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/numerical_computing -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/data_analysis -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} python/build_in_functions -.-> lab-68642{{"`Pandas DataFrame Items Method`"}} end

Import the necessary libraries

First, we need to import the pandas library:

import pandas as pd

Create a DataFrame

Next, let's create a DataFrame with some sample data:

df = pd.DataFrame({
    "Name": ["Navya", "Vindya"],
    "Age": [25, 24],
    "Education": ["M.Tech", "Ph.d"]
}, index=['id001', 'id002'])

Iterate over column names

To iterate over the column names of the DataFrame, we can use the items() method. This method returns a generator object. We can print it or iterate over it using a for loop:

print("Iterating over column names using items():")

for column_name, data in df.items():
    print('Column Name:', column_name)

Iterate over column values

To access the values of each column, we can use the items() method in combination with a for loop. Each iteration will return a tuple of the column name and the corresponding Series:

print("Iterating over column values using items():")

for column_name, data in df.items():
    print('Column Name:', column_name)
    print('Data:', data)

Iterate over a specific row

To iterate over a specific row, we can use the index number and access the row through the Series object:

print("Iterating over a specific row using items():")

for column_name, data in df.items():
    print('Column Name:', column_name)
    print('Data:', data[0])  ## Access the first item of the Series

Iterate over a specific column

To iterate over a specific column, we can use the column name as an index for the data object:

print("Iterating over a specific column using items():")

for column_name, data in df.items():
    print('Column Name:', column_name)
    print('Data:', data['id001'])  ## Access the value at 'id001' index

Summary

In this lab, we learned how to use the items() method in the Pandas library to iterate over a DataFrame's columns. We saw how to iterate over column names, column values, specific rows, and specific columns. The items() method provides a convenient way to explore and manipulate data within a DataFrame.

Other Python Tutorials you may like