Home > Blockchain >  How to filter column based on range of values from another column?
How to filter column based on range of values from another column?

Time:04-11

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).

enter image description here

CodePudding user response:

1st we can use pd.cut , then crosstab

out = pd.crosstab(pd.cut(df['x'], [0,20,....]),df['y'], normalize='index')

CodePudding user response:

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')
  • Related