Home > OS >  Pandas groupby values with bin
Pandas groupby values with bin

Time:04-24

It seems like a simple question, but I need ur help.

For example, I have df:

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 1, 3, 1, 8, 9, 6, 7, 4, 6]

How can I group 'x' in range from 1 to 5, and from 6 to 10 and calc mean 'y' value for this two bins? I expect to get new df like:

x_grpd = [5, 10]
y_grpd = [3, 6.4]

Range of 'x' is given as an example. Ideally i want to be able to set any int value to get different bins quantity.

CodePudding user response:

You can use cut and groupby.mean:

bins = [5, 10]
df2 = (df
 .groupby(pd.cut(df['x'], [0] bins,
                 labels=bins,
                 right=True)) 
 ['y'].mean()
 .reset_index()
)

Output:

    x    y
0   5  3.0
1  10  6.4
  • Related