Home > Software engineering >  how do I calculate the median of the different kinds in this dataframe with median labled by kind an
how do I calculate the median of the different kinds in this dataframe with median labled by kind an

Time:03-24

I want to calculate the median for the waiting based on the column kind of this data frame, then display them as

kind         median
short        55.5
long         81.5

instead I'm getting this

5      55.5
15     55.5
17     81.5
24     81.5
37     81.5
39     81.5
46     55.5
50     81.5
53     81.5
55     81.5
65     81.5

this code is creating this result:

median= df(['kind'])['waiting'].transform('median')

I really just want a single line for each long and short, and for it to be automated.

enter image description here

CodePudding user response:

Try:

median = df.groupby('kind', as_index=False)['waiting'].median()
print(median)

# Output:
    kind  waiting
0   long     36.5
1  short     59.5

Setup:

import pandas as pd
import numpy as np

rng = np.random.default_rng()
df = pd.DataFrame({'kind': np.random.choice(['short', 'long'], 20),
                   'waiting': np.random.randint(0, 100, 20)})
print(df)

# Output
     kind  waiting
0   short       89
1    long       36
2   short       57
3   short       14
4   short       62
5    long       28
6    long       68
7    long       75
8    long       21
9    long       64
10  short       63
11  short       38
12   long       37
13  short       57
14  short       70
15  short       56
16   long       16
17  short       92
18   long       37
19   long       21
  • Related