Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
Last Updated :
30 Nov, 2023
Pandas DataFrames facilitate column-wise iteration, allowing convenient access to elements in each column. In this article, we will discuss how to loop or Iterate overall or certain columns of a DataFrame.
Creating Pandas Dataframe
In this article, we will use this Dataframe that we have created by using the Pandas package.
Python3
import pandas as pd
students = [( 'Ankit' , 22 , 'A' ),
( 'Swapnil' , 22 , 'B' ),
( 'Priya' , 22 , 'B' ),
( 'Shivangi' , 22 , 'B' ),
]
stu_df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'Section' ],
index = [ '1' , '2' , '3' , '4' ])
stu_df
|
Output

Pandas Iterate Over Columns of DataFrame
Below are the ways by which we can iterate over columns of Dataframe in Python Pandas:
- Using Dataframe.iteritems()
- Using [ ] operator
- Iterate over more than one column
- Iterating columns in reverse order
- Using iloc[]
Pandas Iterate Over Columns of DataFrame using DataFrame.iteritems():Â
Dataframe class provides a member function iteritems() which gives an iterator that can be utilized to iterate over all the columns of a data frame. For every column in the Dataframe it returns an iterator to the tuple containing the column name and its contents as series. Here, this code creates a pandas DataFrame named stu_df
from a list of tuples representing student information. It then iterates through the columns of the DataFrame, printing the column names and their corresponding values.
Python3
import pandas as pd
students = [( 'Ankit' , 22 , 'A' ),
( 'Swapnil' , 22 , 'B' ),
( 'Priya' , 22 , 'B' ),
( 'Shivangi' , 22 , 'B' ),
]
stu_df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'Section' ],
index = [ '1' , '2' , '3' , '4' ])
for (columnName, columnData) in stu_df.iteritems():
print ( 'Column Name : ' , columnName)
print ( 'Column Contents : ' , columnData.values)
|
Output

Loop or Iterate Over all or Certain Columns using [ ] operator
We can iterate over column names and select our desired column. Here, the code constructs a pandas DataFrame named stu_df
from a list of tuples, representing student information. It then iterates through the columns, printing each column’s name and its corresponding values using the DataFrame’s column selection with the []
operator.
Python3
import pandas as pd
students = [( 'Ankit' , 22 , 'A' ),
( 'Swapnil' , 22 , 'B' ),
( 'Priya' , 22 , 'B' ),
( 'Shivangi' , 22 , 'B' ),
]
stu_df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'Section' ],
index = [ '1' , '2' , '3' , '4' ])
for column in stu_df:
columnSeriesObj = stu_df[column]
print ( 'Column Name : ' , column)
print ( 'Column Contents : ' , columnSeriesObj.values)
|
Output

Iterate Over More than One Column
Assume we need to iterate more than one column. In order to do that we can choose more than one column from dataframe and iterate over them. Here, the code constructs a pandas DataFrame named stu_df
from a list of tuples, representing student information. It iterates over the specified columns, namely ‘Name’ and ‘Section’, printing each column’s name and its corresponding values using DataFrame’s column selection with the []
operator.
Python3
import pandas as pd
students = [( 'Ankit' , 22 , 'A' ),
( 'Swapnil' , 22 , 'B' ),
( 'Priya' , 22 , 'B' ),
( 'Shivangi' , 22 , 'B' ),
]
stu_df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'Section' ],
index = [ '1' , '2' , '3' , '4' ])
for column in stu_df[[ 'Name' , 'Section' ]]:
columnSeriesObj = stu_df[column]
print ( 'Column Name : ' , column)
print ( 'Column Contents : ' , columnSeriesObj.values)
|
Output

Iterating over Pandas DataFrame in Reversed Order
We can iterate over columns in reverse order as well. Here, the code creates a pandas DataFrame named stu_df
from a list of tuples, representing student information. It iterates over the column names in reverse order, printing each column’s name and its corresponding values using DataFrame’s column selection with the []
operator.
Python3
import pandas as pd
students = [( 'Ankit' , 22 , 'A' ),
( 'Swapnil' , 22 , 'B' ),
( 'Priya' , 22 , 'B' ),
( 'Shivangi' , 22 , 'B' ),
]
stu_df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'Section' ],
index = [ '1' , '2' , '3' , '4' ])
for column in reversed (stu_df.columns):
columnSeriesObj = stu_df[column]
print ( 'Column Name : ' , column)
print ( 'Column Contents : ' , columnSeriesObj.values)
|
Output

Iterate Over all Columns of a Dataframe using Index iloc[]
To iterate over the columns of a Dataframe by index we can iterate over a range i.e. 0 to Max number of columns than for each index we can select the contents of the column using iloc[]. Here, the code creates a pandas DataFrame named stu_df
from a list of tuples, representing student information. It iterates over the column index positions, printing each column’s number and its corresponding values using DataFrame’s iloc[]
method for column selection.
Python3
import pandas as pd
students = [( 'Ankit' , 22 , 'A' ),
( 'Swapnil' , 22 , 'B' ),
( 'Priya' , 22 , 'B' ),
( 'Shivangi' , 22 , 'B' ),
]
stu_df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'Section' ],
index = [ '1' , '2' , '3' , '4' ])
for index in range (stu_df.shape[ 1 ]):
print ( 'Column Number : ' , index)
columnSeriesObj = stu_df.iloc[:, index]
print ( 'Column Contents : ' , columnSeriesObj.values)
|
Output

Similar Reads
Iterating over rows and columns in Pandas DataFrame
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate
7 min read
How to Iterate over rows and columns in PySpark dataframe
In this article, we will discuss how to iterate rows and columns in PySpark dataframe. Create the dataframe for demonstration: C/C++ Code # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an app
6 min read
Get a list of a specified column of a Pandas DataFrame
In data analysis, extracting specific columns from a DataFrame and converting them into Python lists is a common requirement. Pandas provides multiple ways to achieve this efficiently. This article explores various methods to extract a specific column from a Pandas DataFrame and convert it into a li
3 min read
Select all columns, except one given column in a Pandas DataFrame
DataFrame Data structure are the heart of Pandas library. DataFrames are basically two dimension Series object. They have rows and columns with rows representing the index and columns representing the content. Now, let's see how to Select all columns, except one given column in Pandas DataFrame in P
2 min read
How to Show All Columns of a Pandas DataFrame?
Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame. Using pd.set_option to Show All Pandas ColumnsPandas provides a set_option() function that allows you to configure various display options, includi
2 min read
Check whether a given column is present in a Pandas DataFrame or not
Consider a Dataframe with 4 columns : 'ConsumerId', 'CarName', CompanyName, and 'Price'. We have to determine whether a particular column is present in the DataFrame or not in Pandas Dataframe using Python. Creating a Dataframe to check if a column exists in Dataframe C/C++ Code # import pandas libr
2 min read
Pandas filter a dataframe by the sum of rows or columns
In this article, we will see how to filter a Pandas DataFrame by the sum of rows or columns. This can be useful in some conditions. Let's suppose you have a data frame consisting of customers and their purchased fruits. The rows consist of different customers and columns contain different types of f
4 min read
Change column names and row indexes in Pandas DataFrame
Given a Pandas DataFrame, let's see how to change its column names and row indexes. About Pandas DataFramePandas DataFrame are rectangular grids which are used to store data. It is easy to visualize and work with data when stored in dataFrame. It consists of rows and columns.Each row is a measuremen
4 min read
Determine Period Index and Column for DataFrame in Pandas
In Pandas to determine Period Index and Column for Data Frame, we will use the pandas.period_range() method. It is one of the general functions in Pandas that is used to return a fixed frequency PeriodIndex, with day (calendar) as the default frequency. Syntax: pandas.to_numeric(arg, errors=âraiseâ,
2 min read
Merge/Join Two Dataframes on Multiple Columns in Pandas
When working with large datasets, it's common to combine multiple DataFrames based on multiple columns to extract meaningful insights. Pandas provides the merge() function, which enables efficient and flexible merging of DataFrames based on one or more keys. This guide will explore different ways to
6 min read