I want to find counts of my data between certain custom ranges.
Say I have some data:
import random
my_randoms = random.sample(xrange(100), 10)
test = pd.DataFrame(my_randoms,columns = ["x"])
How can I produce a data frame that shows the number of values between different ranges? For example, say I want to see how many values occur between 0-19, 20-39, 40-59, 60-79, 80-100. The output dataframe will have one column with those ranges, another with the counts.
I can think of some ugly approaches that involve use of .apply to get a new column list saying which value they are between (and then doing a groupby), but I suspect pandas has a cleaner way lurking about.
Per Jarad's link to that other question:
test.groupby(pd.cut(test['x'], np.arange(0,100,20))).count()
there's probably a better way. I'm only new to pandas myself but how about this for the moment:
test.query(test.x.isin(range(20)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With