iterate over every rows column in pandas in python

To iterate over rows and columns of a Pandas dataframe, you can use the iterrows() method. Here's an example of how to use it:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'Name': ['John', 'Mary', 'Peter'], 'Age': [27, 25, 32], 'Salary': [50000, 60000, 70000]})

# iterate over rows and columns
for index, row in df.iterrows():
    for col in row.index:
        print(f'{col}: {row[col]}')
286 chars
10 lines

This code will iterate over each row in the dataframe, and for each row it will iterate over each column and print the column name and value. This allows you to perform some operation on each element of the dataframe.

related categories

gistlibby LogSnag