Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

For the following pandas dataframe

    servo_in_position   second_servo_in_position    Expected output

0   0   1   0

1   0   1   0

2   1   2   1

3   0   3   0

4   1   4   2

5   1   4   2

6   0   5   0

7   0   5   0

8   1   6   3

9   0   7   0

10  1   8   4

11  0   9   0

12  1   10  5

13  1   10  5

14  1   10  5

15  0   11  0

16  0   11  0

17  0   11  0

18  1   12  6

19  1   12  6

20  0   13  0

21  0   13  0

22  0   13  0

I want to increment the column "Expected output" only if "servo_in_position" changes from 0 to 1. I want also to assume "Expected output" to be 0 (null) if "servo_in_position" equals to 0.

I tried

input_data['second_servo_in_position']=(input_data.servo_in_position.diff()!=0).cumsum()

but it gives output as in "second_servo_in_position" column, which is not what I wanted.

After that I would like to group and calculate mean using:

print("Mean=\n\n",input_data.groupby('second_servo_in_position').mean())

1 Answer

0 votes
by (25.1k points)

Use cumsum and mask:

df['servo_in_position'].diff().fillna(df['servo_in_position']).eq(1).cumsum().mask(df['servo_in_position'] == 0, 0)

Browse Categories

...