How to Iterate Over Rows in a Pandas DataFrame?

python

Iterate over the rows in Pandas DataFrame using the iterrows method of the data frame.

import pandas as pd

df = pd.DataFrame({'colA': [1, 2, 3], 'colB': [2, 4, 8]})

for index, row in df.iterrows():
    print(row['colA'], row['colB'])

'''
Output:
1 2
2 4
3 8
'''

However, iterating over the rows in Pandas DataFrame is considered an anti-pattern, and is recommended only when there is no other option.

Latest Questions

python How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?