简体   繁体   English

当使用 dataframe 以使其更快时,另一种编写循环的方法和 if 在 python 中

[英]Alternative way of writing for loop and if in python when working with a dataframe to make it faster

I have a data frame named 'plans_to_csv' looking like this:我有一个名为“plans_to_csv”的数据框,如下所示:

在此处输入图像描述

I need to do the following analysis to realize what is the actual mode.我需要做以下分析以了解实际模式是什么。 But this takes so long to run.但这需要很长时间才能运行。 Is there an alternative way for writing this code to make it faster?是否有另一种方法可以编写此代码以使其更快? Thanks a lot for your help in advance.非常感谢您提前提供的帮助。

for i in range (0, len(plans_to_csv)-2):
    if (plans_to_csv['mode'][i+1]=='walk' and plans_to_csv['type'][i+2]=='car interaction' and 
        plans_to_csv['person_id'][i]==plans_to_csv['person_id'][i+2]):

        plans_to_csv['actual_mode_car'][i]=1

You can shift the columsn and do comparisons.您可以移动列并进行比较。 That will make use of vectorization and should be faster.这将利用矢量化并且应该更快。

selection = (plans_to_csv['mode'].shift(-1) == 'walk') & (plans_to_csv['type'].shift(-2)=='car interaction') & (plans_to_csv['person_id'] == plans_to_csv['person_id'].shift(-2))
plans_to_csv['actual_mode_car']= selection.astype(int)

Note that this sets all the entries to 0 that don't match the comparison.请注意,这会将所有与比较不匹配的条目设置为 0。 If this is not wanted, you can just do plans_to_csv['actual_mode_car'][selection]= 1如果不需要,您可以执行 plans_to_csv['actual_mode_car'][selection]= 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM