gpt4 book ai didi

python - 如何在Python的条件下增加数据帧列?

转载 作者:行者123 更新时间:2023-12-01 01:41:41 25 4
gpt4 key购买 nike

我有这个数据框 df

                       delta_clean  delta_time  new_index
2017-05-24 11:59:29 -2.837001e-01 1.472778 0
2017-05-24 13:02:18 -2.546754e-02 1.046944 0
2017-05-24 14:05:06 -1.039444e-01 1.046667 0
2017-05-24 15:29:12 5.573439e+00 1.401667 0
2017-05-24 16:32:02 -1.567387e-01 1.047222 0
2017-05-24 17:34:52 -8.079458e-02 1.047222 0
2017-05-24 18:37:41 -5.220998e-02 1.046944 0
2017-05-24 19:40:31 -2.302334e-02 1.047222 0
2017-05-24 20:43:20 -4.382400e-02 1.046944 0
2017-05-24 21:46:10 -1.675438e-02 1.047222 0
2017-05-24 22:49:01 -5.994632e-02 1.047500 0
2017-05-24 23:51:53 -4.845164e-02 1.047778 0
2017-05-24 23:59:59 -2.226252e-02 0.135000 0
2017-05-25 00:00:01 -9.161529e-05 0.000556 0
2017-05-25 00:54:45 -1.504323e-01 0.912222 0
2017-05-25 01:57:38 -5.539247e-02 1.048056 0
2017-05-25 06:34:57 -2.389934e-01 4.621944 0
2017-05-25 07:37:48 1.037526e-01 1.047500 0
2017-05-25 08:40:38 -2.081650e-01 1.047222 0
2017-05-25 09:43:27 -5.335963e-02 1.046944 0
2017-05-25 10:46:16 -8.687169e-02 1.046944 0
2017-05-25 11:49:05 -2.209528e-01 1.046944 0
2017-05-25 12:51:54 -4.416145e-02 1.046944 0
2017-05-25 13:54:43 -3.051953e-02 1.046944 0
2017-05-25 14:57:31 -4.792480e-02 1.046667 0
2017-05-25 16:00:20 -1.288275e-02 1.046944 0
2017-05-25 17:03:09 3.362946e-02 1.046944 0
2017-05-25 18:05:59 -6.533723e-02 1.047222 0
2017-05-25 19:08:49 -6.509860e-02 1.047222 0

每次找到此条件时,我都需要df['delta_clean']/df['delta_time']>2,以增加新索引的值我该如何继续?

最佳答案

累计增加您的系列,您可以使用pd.Series.cumsum :

df['new_index'] = (df['delta_clean'] / df['delta_time'] > 2).cumsum()

这是有效的,因为 bool 系列中的 True 值,例如 (df['delta_clean']/df['delta_time'] > 2) 被解释为 1 用于数值计算。类似于常规 Python,其中 boolint 的子类。

关于python - 如何在Python的条件下增加数据帧列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51819367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com