import pandas as pd import numpy as np
raw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'], 'age': [20, 19, 22, 21], 'favorite_color': ['blue', 'red', 'yellow', "green"], 'grade': [88, 92, 95, 70]} df = pd.DataFrame(raw_data, columns = ['name', 'age', 'favorite_color', 'grade']) df
Using iterrows:
for index, row in df.iterrows(): print (row["name"], row["age"])
Willard Morris 20 Al Jennings 19 Omar Mullins 22 Spencer McDaniel 21
Using itertuples:
for row in df.itertuples(index=True, name='Pandas'): print (getattr(row, "name"), getattr(row, "age"))
If you wish to modify the rows you're iterating over, then df.apply is preferred:
def valuation_formula(x): return x * 0.5 df['age_half'] = df.apply(lambda row: valuation_formula(row['age']), axis=1) df.head()