如何在python中的条件下增加dataframe列?

时间:2018-08-13 09:38:02

标签: python pandas dataframe

我有这个数据框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时都需要增加新索引的值 我该怎么办?

1 个答案:

答案 0 :(得分:1)

累积增加序列号,可以使用pd.Series.cumsum

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

之所以可行,是因为在进行数值计算时,布尔系列(例如True)中的(df['delta_clean'] / df['delta_time'] > 2)个值被解释为1。类似于常规Python,其中boolint的子类。