python - apply pandas qcut function to subgroups -
let assume created dataframe df using code below. have created bin frequency count based on 'value' column in df. how frequency count of these label=1 samples frequency count based on previous created bin? obviously, should not use qcut label = 1 samples count, since bin positions not same before.
import numpy np import pandas pd mu, sigma = 0, 0.1 theta = 0.3 s = np.random.normal(mu, sigma, 100) group = np.random.binomial(1, theta, 100) df = pd.dataframe(np.vstack([s,group]).transpose()) df.columns = ['value','label'] factor = pd.qcut(df['value'], 5) factor_bin_count = pd.value_counts(factor) update: took solution jeff
df.groupby(['label',factor]).value.count()
if understand question. want take grouping factor (e.g. created using qcut bin continuous values), , grouper (e.g. 'label'), perform operation. count in case.
in [36]: df.groupby(['label',factor]).value.count() out[36]: label value 0 [-0.248, -0.0864] 14 (-0.0864, -0.0227] 15 (-0.0227, 0.0208] 15 (0.0208, 0.0718] 17 (0.0718, 0.24] 13 1 [-0.248, -0.0864] 6 (-0.0864, -0.0227] 5 (-0.0227, 0.0208] 5 (0.0208, 0.0718] 3 (0.0718, 0.24] 7 name: value, dtype: int64
Comments
Post a Comment