Home > Software engineering >  How to sum the result of a Pandas Groupby based on the index value of the groupby
How to sum the result of a Pandas Groupby based on the index value of the groupby

Time:10-10

I can't figure out how to sum up a part of the results of a Pandas value_counts(). In this case: I need the sum of the values until index 8 (as in the result of the value_counts() (wicht is a long series) Hopefully some can help me on this. Thank you all in advance.

I perform a value_count on one of my df columns with:

df_v2.Q_score_diff.value_counts().sort_index(ascending=False)T

The resulting Series: of the value_counts: 4

8.0       2
 47.0       6
 46.0      21
 45.0      47
 44.0     144
 43.0     251
 42.0     384
 41.0     597
 40.0     783
 39.0     947
 38.0    1225
 37.0    1501
 36.0    1822
 35.0    2062
 34.0    2312
 33.0    2662
 32.0    2907
 31.0    3123
 30.0    3349
 29.0    3558
 28.0    3862
 27.0    3734
 26.0    3878
 25.0    3969
 24.0    3997
 23.0    3914
 22.0    3907
 21.0    3866
 20.0    3624
 19.0    3519
 18.0    3396
 17.0    3147
 16.0    2894
 15.0    2701
 14.0    2475
 13.0    2278
 12.0    2077
 11.0    1881
 10.0    1611
 9.0     1408
 8.0     1304
 7.0     1182
 6.0     1042
 5.0      845
 4.0      735
 3.0      722
 2.0      615
 1.0      534
 0.0      505
-1.0      383
-2.0      330
-3.0      284
-4.0      227
-5.0      202
-6.0      148
-7.0      139
-8.0      112
-9.0       96
-10.0      65
-11.0      53
-12.0      46
-13.0      47
-14.0      31
-15.0      22
-16.0      19
-17.0      18
-18.0      12
-19.0      18
-20.0       8
-21.0       1
-22.0      10
-23.0       5
-24.0       7
-25.0       5
-26.0       2
-27.0       2
-28.0       2
-29.0       3
-32.0       4
-34.0       1
-35.0       2
-40.0       1

CodePudding user response:

Is it what your are looking for:

>>> df.loc[:8.0]
47.0     6
46.0    21
45.0    47
44.0   144
43.0   251
42.0   384
41.0   597
40.0   783
39.0   947
38.0  1225
37.0  1501
36.0  1822
35.0  2062
34.0  2312
33.0  2662
32.0  2907
31.0  3123
30.0  3349
29.0  3558
28.0  3862
27.0  3734
26.0  3878
25.0  3969
24.0  3997
23.0  3914
22.0  3907
21.0  3866
20.0  3624
19.0  3519
18.0  3396
17.0  3147
16.0  2894
15.0  2701
14.0  2475
13.0  2278
12.0  2077
11.0  1881
10.0  1611
9.0   1408
8.0   1304
  • Related