Conditionally Binning
Is it possible to create a new column in a dataframe where the bins for 'X' are based on a value of another column(s). Example below. The bins for AR1, PO1 and RU1 are different f
Solution 1:
Simple use pd.qcut
within a groupby
on S
df['bins_X'] = df.groupby('S').X.apply(pd.qcut, q=10, labels=np.arange(10))
df.groupby(['bins_X', 'S']).size().unstack()
S AR1 PO1 RU1
bins_X
0 3 4 4
1 3 3 4
2 3 3 4
3 2 3 4
4 3 4 4
5 3 3 3
6 2 3 4
7 3 3 4
8 3 3 4
9 3 4 4
Leave of the labels
parameter if you want them to have their own unique edges
df['bins_X'] = df.groupby('S').X.apply(pd.qcut, q=10)
Post a Comment for "Conditionally Binning"