Iterate over a DataFrame

Create a sample dataframe

# Import modules
import pandas as pd
# Example dataframe

raw_data  = {'fruit': ['Banana', 'Orange', 'Apple', 'lemon', "lime", "plum"], 
        'color': ['yellow', 'orange', 'red', 'yellow', "green", "purple"], 
        'kcal': [89, 47, 52, 15, 30, 28]
    }

df = pd.DataFrame(raw_data, columns = ['fruit', 'color', 'kcal'])
df
fruit color kcal
0 Banana yellow 89
1 Orange orange 47
2 Apple red 52
3 lemon yellow 15
4 lime green 30
5 plum purple 28

Using the iterrows method

Pandas DataFrames can return a generator with the iterrrows method. It can then be used to loop over the rows of the DataFrame

for index, row in df.iterrows():
    print("At line {0} there is a {1} which is {2} and contains {3} kcal".format(index, row["fruit"], row["color"], row["kcal"]))
At line 0 there is a Banana which is yellow and contains 89 kcal
At line 1 there is a Orange which is orange and contains 47 kcal
At line 2 there is a Apple which is red and contains 52 kcal
At line 3 there is a lemon which is yellow and contains 15 kcal
At line 4 there is a lime which is green and contains 30 kcal
At line 5 there is a plum which is purple and contains 28 kcal