• python笔记:pandas/geopandas DataFrame逐行遍历


    Pandas和GeoPandas中,可以使用几种不同的方法来遍历DataFrame的每一行

    0 数据

    1. import pandas as pd
    2. data = {
    3. 'column1': range(1, 1001),
    4. 'column2': range(1001, 2001)
    5. }
    6. df = pd.DataFrame(data)
    7. df

     

    1 iterrows

    1. for index, row in df.iterrows():
    2. print(index)
    3. print(row)
    4. '''
    5. 0
    6. column1 1
    7. column2 1001
    8. Name: 0, dtype: int64
    9. 1
    10. column1 2
    11. column2 1002
    12. Name: 1, dtype: int64
    13. 2
    14. column1 3
    15. column2 1003
    16. Name: 2, dtype: int64
    17. 3
    18. column1 4
    19. column2 1004
    20. Name: 3, dtype: int64
    21. ...
    22. '''
    • 优点:简单直观,可以同时获取行索引和数据。
    • 缺点:比其他方法慢,尤其是在大数据集上,因为它逐行遍历。

    2 itertuples

    1. for row in df.itertuples():
    2. print(row)
    3. print(row.Index)
    4. print(row.column1)
    5. print(row.column2)
    6. '''
    7. Pandas(Index=0, column1=1, column2=1001)
    8. 0
    9. 1
    10. 1001
    11. Pandas(Index=1, column1=2, column2=1002)
    12. 1
    13. 2
    14. 1002
    15. Pandas(Index=2, column1=3, column2=1003)
    16. 2
    17. 3
    18. 1003
    19. ...
    20. '''
    • 优点:比 iterrows() 快,因为它返回命名元组,遍历的是元组而不是Series对象。
    • 缺点:仍然比向量化操作慢,稍微复杂一点。

    3 apply

    1. def process_row(row):
    2. print(row)
    3. df.apply(process_row, axis=1)
    4. '''
    5. column1 1
    6. column2 1001
    7. Name: 0, dtype: int64
    8. column1 2
    9. column2 1002
    10. Name: 1, dtype: int64
    11. column1 3
    12. column2 1003
    13. Name: 2, dtype: int64
    14. ...
    15. '''
    • 优点:可以方便地应用一个函数到每一行或每一列。
    • 缺点:比 itertuples() 慢,而且在使用上可能比直接遍历更复杂一些。

    4 applymap

    1. def process_row(element):
    2. print(element)
    3. df.applymap(process_row)
    4. '''
    5. 1
    6. 2
    7. 3
    8. 4
    9. 5
    10. 6
    11. 7
    12. 8
    13. 9
    14. 10
    15. ...
    16. '''
    • 优点:可以方便地应用一个函数到DataFrame的每个元素。
    • 缺点:可能不如其他方法高效,尤其是在大数据集上。

    5 逐元素at

    1. for i in range(len(df)):
    2. print(df.at[i,'column1'],df.at[i,'column2'])
    3. '''
    4. 1 1001
    5. 2 1002
    6. 3 1003
    7. 4 1004
    8. 5 1005
    9. ...
    10. '''

    6 使用timeit 分别计算运行时间

    python 笔记: timeit (测量代码运行时间)-CSDN博客zhiguan

    1. import timeit
    2. def row_at(df):
    3. for i in range(len(df)):
    4. df.at[i,'column1']
    5. df.at[i,'column2']
    6. def iter_row(df):
    7. for index,row in df.iterrows():
    8. index
    9. row
    10. def iter_tuple(df):
    11. for row in df.itertuples():
    12. row
    13. def apply_df(df):
    14. df.apply(lambda x:x,axis=1)
    15. def apply_map_df(df):
    16. df.applymap(lambda x:x)
    17. time_at=timeit.timeit("row_at(df)", globals=globals(),number=1000)
    18. time_iterrow=timeit.timeit('iter_row(df)',globals=globals(),number=1000)
    19. time_itertuple=timeit.timeit('iter_tuple(df)',globals=globals(),number=1000)
    20. time_apply=timeit.timeit('apply_df(df)',globals=globals(),number=1000)
    21. time_applymap=timeit.timeit('apply_map_df(df)',globals=globals(),number=1000)
    22. time_at,time_iterrow,time_itertuple,time_apply,time_applymap
    23. '''
    24. (4.100567077999585,
    25. 14.672198772001138,
    26. 0.37428459300281247,
    27. 12.572721185002592,
    28. 0.5845120449957903)
    29. '''

    直观可视化 

    1. import seaborn as sns
    2. import matplotlib.pyplot as plt
    3. x = ['at by row','iterrows','itertuples','apply','applymap']
    4. y = [time_at,time_iterrow,time_itertuple,time_apply,time_applymap] # 请将这些值替换为你实际的时间数据
    5. sns.barplot(x=x, y=y)
    6. # 创建 barplot
    7. for i, val in enumerate(y):
    8. plt.text(i, val + 0.01, round(val, 2), ha='center')
    9. # 添加标签(x轴、y轴、text的label)
    10. # 显示图形
    11. plt.show()

  • 相关阅读:
    实用技能 | 推荐6款论文一键AI改写修改网站
    MySQL常见数据类型、特点以及使用场景
    Day 21 LAMP架构和DNS域名
    【SLAM】10.纵观SLAM,对比方案和未来方向
    【MySQL】SQL常用函数总结
    ArcGIS Map Sdk for unity使用
    css超出显示
    A Guide to PriorityQueue
    设计师资格证
    服务器安装mysql后无法远程连接
  • 原文地址:https://blog.csdn.net/qq_40206371/article/details/133577662