Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

python - Tracking Changes in Categorical Dataframe Column

I have a dataframe that tracks when daytime rules or nighttime rules are in effect (independent of the actual time of day). I need to track when the dataframe switches from daytime to to nighttime rules, and vice versa.

My new column would display day_1 for the first set of day in rules_in_effect Then, when rules_in_effect switches from day to night, the new column would display the change as night_1. Then, day_2, night_2 and so on for each corresponding switch.

I tried categorically differencing them and cumulatively summing the categorical changes, however that just gives me a summation.

How do I add this new column? Thank you for your help in advance.


                  date rules_in_effect
0  2021-01-04 07:00:00         day
1  2021-01-04 08:00:00         day
2  2021-01-04 09:00:00         day
3  2021-01-04 10:00:00         day
4  2021-01-04 11:00:00         day
5  2021-01-04 12:00:00         day
6  2021-01-04 13:00:00         day
7  2021-01-04 14:00:00         day
8  2021-01-04 15:00:00         day
9  2021-01-04 16:00:00         day
10 2021-01-04 17:00:00         day
11 2021-01-04 18:00:00       night
12 2021-01-04 19:00:00       night
13 2021-01-04 20:00:00       night
14 2021-01-04 21:00:00       night
15 2021-01-04 22:00:00       night
16 2021-01-04 23:00:00       night
17 2021-01-05 00:00:00       night
18 2021-01-05 01:00:00       night
19 2021-01-05 02:00:00       night
20 2021-01-05 03:00:00       night
21 2021-01-05 04:00:00       night
22 2021-01-05 05:00:00       night
23 2021-01-05 06:00:00       night
24 2021-01-05 07:00:00         day
25 2021-01-05 08:00:00         day
26 2021-01-05 09:00:00         day
27 2021-01-05 10:00:00         day
28 2021-01-05 11:00:00         day
29 2021-01-05 12:00:00         day
30 2021-01-05 13:00:00         day
31 2021-01-05 14:00:00         day
32 2021-01-05 15:00:00         day
33 2021-01-05 16:00:00         day
34 2021-01-05 17:00:00         day
35 2021-01-05 18:00:00       night
36 2021-01-05 19:00:00       night
37 2021-01-05 20:00:00       night
38 2021-01-05 21:00:00       night
etc...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try this:

df['newcol'] = (df['rules_in_effect'].astype(str) + 
                "_" + 
                (((df['rules_in_effect'] != df['rules_in_effect'].shift()).cumsum() + 1) // 2)
                   .astype(str)
               )

Output:

                   date rules_in_effect   newcol
0   2021-01-04 07:00:00             day    day_1
1   2021-01-04 08:00:00             day    day_1
2   2021-01-04 09:00:00             day    day_1
3   2021-01-04 10:00:00             day    day_1
4   2021-01-04 11:00:00             day    day_1
5   2021-01-04 12:00:00             day    day_1
6   2021-01-04 13:00:00             day    day_1
7   2021-01-04 14:00:00             day    day_1
8   2021-01-04 15:00:00             day    day_1
9   2021-01-04 16:00:00             day    day_1
10  2021-01-04 17:00:00             day    day_1
11  2021-01-04 18:00:00           night  night_1
12  2021-01-04 19:00:00           night  night_1
13  2021-01-04 20:00:00           night  night_1
14  2021-01-04 21:00:00           night  night_1
15  2021-01-04 22:00:00           night  night_1
16  2021-01-04 23:00:00           night  night_1
17  2021-01-05 00:00:00           night  night_1
18  2021-01-05 01:00:00           night  night_1
19  2021-01-05 02:00:00           night  night_1
20  2021-01-05 03:00:00           night  night_1
21  2021-01-05 04:00:00           night  night_1
22  2021-01-05 05:00:00           night  night_1
23  2021-01-05 06:00:00           night  night_1
24  2021-01-05 07:00:00             day    day_2
25  2021-01-05 08:00:00             day    day_2
26  2021-01-05 09:00:00             day    day_2
27  2021-01-05 10:00:00             day    day_2
28  2021-01-05 11:00:00             day    day_2
29  2021-01-05 12:00:00             day    day_2
30  2021-01-05 13:00:00             day    day_2
31  2021-01-05 14:00:00             day    day_2
32  2021-01-05 15:00:00             day    day_2
33  2021-01-05 16:00:00             day    day_2
34  2021-01-05 17:00:00             day    day_2
35  2021-01-05 18:00:00           night  night_2
36  2021-01-05 19:00:00           night  night_2
37  2021-01-05 20:00:00           night  night_2
38  2021-01-05 21:00:00           night  night_2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...