1

I have this data frame 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

I need that every time I find this condition df['delta_clean']/df['delta_time']>2, to increment the value of new index how can I proceed?

1 Answer 1

2

To cumulatively increment your series, you can use pd.Series.cumsum:

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

This works because True values in a Boolean series such as (df['delta_clean'] / df['delta_time'] > 2) are interpreted as 1 for numeric calculations. Akin to regular Python, where bool is a subclass of int.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.