DataFrame.iterrows

以(index, Series)遍历DataFrame的行。

Yields

index 行的索引。MultiIndex的元组
data 行的数据,作为一个Series
  1. import pandas as pd
  2. df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
  3. 'age':[18, 39, 22, 45],
  4. 'price': [1.0, 2.0, 3.0, 4.0],
  5. 'color': ['red', 'black', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
  6. for index, row in df.iterrows():
  7. print(index)
  8. print(row)
  9. -------------------------------------------
  10. first
  11. site google
  12. age 18
  13. price 1
  14. color red
  15. Name: first, dtype: object
  16. second
  17. site baidu
  18. age 39
  19. price 2
  20. color black
  21. Name: second, dtype: object
  22. third
  23. site wiki
  24. age 22
  25. price 3
  26. color blue
  27. Name: third, dtype: object
  28. forth
  29. site pandas
  30. age 45
  31. price 4
  32. color red
  33. Name: forth, dtype: object