Heisenberg
Heisenberg

Reputation: 5299

Binning and transforming in pandas

I have dataframe below.

A  B
1  1
4  1
5  1
6  2
8  3
15 4 

I would like to binning and transform dataframe to below.

range(A) sum(B)
[0,4)    2
[5,9)    6
[10,14)  0
[15,19)  4

I tried groupby+Grouper, but I couldnt figure out how to bin.

Can I ask how to transform data?

Upvotes: 3

Views: 387

Answers (1)

jezrael
jezrael

Reputation: 862691

You need cut with parameters right=False for not include right bins with groupby:

Thank you John Galt for idea add parameter labels.

print (pd.cut(df['A'], 
              bins=[0, 5, 10, 15, 20], 
              labels=['[0,4)', '[5,9)', '[10,14)', '[15,19)'], 
              right=False))
0      [0,4)
1      [0,4)
2      [5,9)
3      [5,9)
4      [5,9)
5    [15,19)
Name: A, dtype: category

df = df.groupby([pd.cut(df['A'], bins=[0, 5, 10, 15, 20], 
                        labels=['[0,4)', '[5,9)', '[10,14)', '[15,19)'], 
                        right=False)])
       .B.sum()
       .fillna(0)
       .reset_index()

df.columns = ['range(A)','sum(B)']
print (df)
  range(A)  sum(B)
0    [0,4)     2.0
1    [5,9)     6.0
2  [10,14)     0.0
3  [15,19)     4.0

Upvotes: 4

Related Questions