遍历DataFrame的三种方法

  • iteritem()方法返回一个数据,可利用for循环获得输出
  • iterrow()方法返回一个数据,可利用for循环获得输出
  • itertuple()方法返回一个数据,可利用getattr(row,‘列索引’)方法获得对应数据

    按列遍历

    1. column_indexs = []
    2. for column_index, row_data in frame.iteritems():
    3. column_indexs.append(column_index)
    4. print(row_data)
    5. print(column_indexs)

    运行结果

    1. 0 Ohio
    2. 1 Ohio
    3. 2 Ohio
    4. 3 Nevada
    5. 4 Nevada
    6. 5 Nevada
    7. Name: state, dtype: object
    8. 0 2000
    9. 1 2001
    10. 2 2002
    11. 3 2003
    12. 4 2004
    13. 5 2005
    14. Name: year, dtype: int64
    15. 0 1.5
    16. 1 1.7
    17. 2 3.6
    18. 3 2.4
    19. 4 2.9
    20. 5 3.2
    21. Name: pop, dtype: float64
    22. ['state', 'year', 'pop']

    按行遍历

    方法一:

    1. row_indexs = []
    2. for index, row in frame.iterrows():
    3. row_indexs.append(index)
    4. print(row['pop'])
    5. print(row)
    6. print(row_indexs)

    运行结果

    1. state Ohio
    2. year 2000
    3. pop 1.5
    4. Name: 0, dtype: object
    5. state Ohio
    6. year 2001
    7. pop 1.7
    8. Name: 1, dtype: object
    9. state Ohio
    10. year 2002
    11. pop 3.6
    12. Name: 2, dtype: object
    13. state Nevada
    14. year 2003
    15. pop 2.4
    16. Name: 3, dtype: object
    17. state Nevada
    18. year 2004
    19. pop 2.9
    20. Name: 4, dtype: object
    21. state Nevada
    22. year 2005
    23. pop 3.2
    24. Name: 5, dtype: object
    25. [0, 1, 2, 3, 4, 5]

    方法二:

    1. for row in frame.itertuples():
    2. print(getattr(row, 'state'), getattr(row, 'year'), getattr(row, 'pop'))
    3. print(type(row))
    1. Ohio 2000 1.5
    2. <class 'pandas.core.frame.Pandas'>
    3. ......
    4. Nevada 2005 3.2
    5. <class 'pandas.core.frame.Pandas'>

    获取某个值

  • DataFrame.at[行索引,列索引]获取某一个值:

    1. print(frame.at[1,'pop'])
  • DataFrame.iat[默认行索引,默认列索引]获取某一个值:

    1. print(frame.iat[1,1])