9951 explained code solutions for 126 technologies


python-pandasHow to iterate dataframe rows using iterrows()


import pandas as pd

df = pd.DataFrame({
  'phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'price': [204, 704, 404, 405, 305]
})

for index, series in df.iterrows():
  print(index, series['phone'], series['price'])ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.iterrows()

allows iterating over dataframe rows as a pair of index and series

index

will contain row index

series

will contain row columns values


Usage example

import pandas as pd

df = pd.DataFrame({
  'phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'price': [204, 704, 404, 405, 305]
})

for index, series in df.iterrows():
  print(index, series['phone'], series['price'])
output
0 ip5 204
1 ip6 704
2 ip8 404
3 sms 405
4 xi 305