Reputation: 151
I have a df like below, where y is the target (either 0 or 1). How do I find what percentage of y is either 0 or 1, based on a range of values of my x?
Example: For values of x between 0 and 20, the corresponding y is 10% 1 (and 90% 0).
Upvotes: 0
Views: 104
Reputation: 1651
I hope you like one-liners. Assuming a
is your dataframe:
a.loc[(a.x < 20) & (a.x > 0),'y'].value_counts(normalize=True).to_frame('frequency')
Upvotes: 1
Reputation: 323236
1st we can use pd.cut
, then crosstab
out = pd.crosstab(pd.cut(df['x'], [0,20,....]),df['y'], normalize='index')
Upvotes: 1