繁华开满天机
如果我理解正确的话:# Precompute bins for pd.cutbins = list(range(0, df['Height (m)'].max() + 5, 5))# Cut Height into intervals which exclude the right endpoint, # with bin edges at multiples of 5df['HeightBin'] = pd.cut(df['Height (m)'], bins=bins, right=False)# Within each bin, get mean, stdev (normalized by N-1 by default),# and also show sample size to explain why some std values are NaNdf.groupby('HeightBin')['My data'].agg(['mean', 'std', 'count']) mean std countHeightBin[0, 5) NaN NaN 0[5, 10) 2.00 0.000000 2[10, 15) 1.25 0.353553 2[15, 20) 5.00 NaN 1[20, 25) 5.00 NaN 1[25, 30) 6.35 0.494975 2[30, 35) 8.00 NaN 1