sputniza
sputniza

Reputation: 31

After Pandas groupby.value_counts() how can I filter on the result?

im still quite new to pandas.. I managed to receive a Series with the following groupby clause:

data.groupby("salary_percentile").early_quitter.value_counts(normalize = True)
Out:
    salary_percentile  early_quitter
[17000, 25000]     0                0.744526
                   1                0.255474
(25000, 30000]     0                0.751880
                   1                0.248120
(30000, 38000]     0                0.701799
                   1                0.298201
(38000, 46000]     0                0.772118
                   1                0.227882
(46000, 53000]     0                0.742015
                   1                0.257985
(53000, 57000]     0                0.720339
                   1                0.279661
(57000, 61000]     0                0.745763
                   1                0.254237
(61000, 64000]     0                0.688144
                   1                0.311856

I do only need the salary_percentile and the respective second value of percentages (where the early quitter columns is 1) - how would I do this properly?

Upvotes: 0

Views: 299

Answers (1)

jezrael
jezrael

Reputation: 862581

The simpliest is xs:

df = df.xs(1, level=1)
print (df)

salary_percentile
[17000, 25000]    0.255474
(25000,30000]     0.248120
(30000,38000]     0.298201
Name: val, dtype: float64

Upvotes: 1

Related Questions