And I want to change index by "video_second" value.
conditions=[(df['video_second'] == 612.00),
(df['video_second'] == 612.05),
(df['video_second'] == 612.10),
(df['video_second'] == 612.15),
(df['video_second'] == 612.20),
(df['video_second'] == 612.25),
(df['video_second'] == 612.30),
(df['video_second'] == 612.35),
(df['video_second'] == 612.40),
(df['video_second'] == 612.45),
(df['video_second'] == 612.50),
(df['video_second'] == 612.55),
(df['video_second'] == 612.60),
(df['video_second'] == 612.65),
(df['video_second'] == 612.70),
(df['video_second'] == 612.75),
(df['video_second'] == 612.80),
(df['video_second'] == 612.85),
(df['video_second'] == 612.90),
(df['video_second'] == 612.95),
(df['video_second'] == 613.00),
(df['video_second'] == 613.05),
(df['video_second'] == 613.10),
(df['video_second'] == 613.15),
(df['video_second'] == 613.20),
(df['video_second'] == 613.25),
(df['video_second'] == 613.30),
(df['video_second'] == 613.35),
(df['video_second'] == 613.40),
(df['video_second'] == 613.45),
(df['video_second'] == 613.50),
(df['video_second'] == 613.55),
(df['video_second'] == 613.60),
(df['video_second'] == 613.65),
(df['video_second'] == 613.70),
(df['video_second'] == 613.75),
(df['video_second'] == 613.80),
(df['video_second'] == 613.85),
(df['video_second'] == 613.90),
(df['video_second'] == 613.95),
(df['video_second'] == 614.00),
(df['video_second'] == 614.05),
..........
(df['video_second'] == 617.45),
(df['video_second'] == 617.50),
(df['video_second'] == 617.55),
(df['video_second'] == 617.60),
(df['video_second'] == 617.65),
(df['video_second'] == 617.70),
(df['video_second'] == 617.75),
(df['video_second'] == 617.80),
(df['video_second'] == 617.85),
(df['video_second'] == 617.90),
(df['video_second'] == 617.95),
(df['video_second'] == 618.00)]
choices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120]
df['frame'] = np.select(conditions, choices)
df.index = df["frame"]
df.drop("frame", axis=1, inplace=True)
df=df.sort_index()
After that, it works and my dataframe looks like that:
But I want to automatize this work. Manually, it is easy to do but so much waste of time. And I should modify that long code for every other seconds. So, any easy/automatize way to do that?
CodePudding user response:
video_seconds = np.arange(612.00, 618.05, 0.05)
choices = np.arange(len(video_seconds))
df.index = df['video_second'].replace(video_seconds, choices)
then you are sorting, which can also be done in-place:
df.sort_index(inplace=True)
CodePudding user response:
Here is my sample df
id video_second
0 350 612.00
1 23 612.10
2 208 612.00
3 65 613.00
4 315 612.02
5 25 612.10
6 968 612.04
7 351 612.20
8 45 612.06
9 85 612.00
10 62 613.00
11 358 614.00
12 78 612.06
13 45 612.30
14 62 612.80
15 56 614.00
You need to sort
the values then apply factorize
. I have made the index start from 1 but you can change that to 0 by take 1
after the factorize
df = df.sort_values(['video_second'])
df['idx'] = df['video_second'].factorize()[0] 1
This is the output I got
id video_second idx
0 350 612.00 1
2 208 612.00 1
9 85 612.00 1
4 315 612.02 2
6 968 612.04 3
8 45 612.06 4
12 78 612.06 4
1 23 612.10 5
5 25 612.10 5
7 351 612.20 6
13 45 612.30 7
14 62 612.80 8
3 65 613.00 9
10 62 613.00 9
11 358 614.00 10
15 56 614.00 10