Open In App

Loop or Iterate over all or certain columns of a dataframe in Python-Pandas

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 package
import pandas as pd
 
# List of Tuples
students = [('Ankit', 22, 'A'),
            ('Swapnil', 22, 'B'),
            ('Priya', 22, 'B'),
            ('Shivangi', 22, 'B'),
            ]
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns=['Name', 'Age', 'Section'],
                      index=['1', '2', '3', '4'])
 
stu_df


Output

pandas-iterate

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
 
# List of Tuples
students = [('Ankit', 22, 'A'),
            ('Swapnil', 22, 'B'),
            ('Priya', 22, 'B'),
            ('Shivangi', 22, 'B'),
            ]
 
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns=['Name', 'Age', 'Section'],
                      index=['1', '2', '3', '4'])
 
# gives a tuple of column name and series
# for each column in the dataframe
for (columnName, columnData) in stu_df.iteritems():
    print('Column Name : ', columnName)
    print('Column Contents : ', columnData.values)


Output

iterate over columns in dataframe-1

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
 
# List of Tuples
students = [('Ankit', 22, 'A'),
            ('Swapnil', 22, 'B'),
            ('Priya', 22, 'B'),
            ('Shivangi', 22, 'B'),
            ]
 
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns=['Name', 'Age', 'Section'],
                      index=['1', '2', '3', '4'])
 
# Iterate over column names
for column in stu_df:
 
    # Select column contents by column
    # name using [] operator
    columnSeriesObj = stu_df[column]
    print('Column Name : ', column)
    print('Column Contents : ', columnSeriesObj.values)


Output

iterate over columns in dataframe-2

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
 
# List of Tuples
students = [('Ankit', 22, 'A'),
            ('Swapnil', 22, 'B'),
            ('Priya', 22, 'B'),
            ('Shivangi', 22, 'B'),
            ]
 
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns=['Name', 'Age', 'Section'],
                      index=['1', '2', '3', '4'])
 
# Iterate over two given columns
# only from the dataframe
for column in stu_df[['Name', 'Section']]:
 
    # Select column contents by column
    # name using [] operator
    columnSeriesObj = stu_df[column]
    print('Column Name : ', column)
    print('Column Contents : ', columnSeriesObj.values)


Output

iterate over columns in dataframe-3

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
 
# List of Tuples
students = [('Ankit', 22, 'A'),
            ('Swapnil', 22, 'B'),
            ('Priya', 22, 'B'),
            ('Shivangi', 22, 'B'),
            ]
 
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns=['Name', 'Age', 'Section'],
                      index=['1', '2', '3', '4'])
 
# Iterate over the sequence of column names
# in reverse order
for column in reversed(stu_df.columns):
 
    # Select column contents by column
    # name using [] operator
    columnSeriesObj = stu_df[column]
    print('Column Name : ', column)
    print('Column Contents : ', columnSeriesObj.values)


Output

iterate over columns in dataframe-4

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
 
# List of Tuples
students = [('Ankit', 22, 'A'),
            ('Swapnil', 22, 'B'),
            ('Priya', 22, 'B'),
            ('Shivangi', 22, 'B'),
            ]
 
# Create a DataFrame object
stu_df = pd.DataFrame(students, columns=['Name', 'Age', 'Section'],
                      index=['1', '2', '3', '4'])
 
# Iterate over the index range from
# 0 to max number of columns in dataframe
for index in range(stu_df.shape[1]):
 
    print('Column Number : ', index)
 
    # Select column by index position using iloc[]
    columnSeriesObj = stu_df.iloc[:, index]
    print('Column Contents : ', columnSeriesObj.values)


Output

iterate over columns in dataframe-5



Last Updated : 30 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads