Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby value_count filter by frequency

I would like to filter out the frequencies that are less than n, in my case n is 2

df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'bar',],'B' : ['yes', 'no', 'yes', 'no', 'no', 'yes','yes', 'no', 'no', 'no']})
df.groupby('A')['B'].value_counts()

A    B  
bar  no     4
     yes    1
foo  yes    3
     no     2
Name: B, dtype: int64

Ideally I would like the results in a dataframe showing the below(frequency of 1 is not excluded)

A    B      freq
bar  no     4
foo  yes    3
foo  no     2

I have tried

df.groupby('A')['B'].filter(lambda x: len(x) > 1)

but this fails as apparently groupby returns a serie

like image 465
Guillaume Lombard Avatar asked Oct 20 '25 04:10

Guillaume Lombard


2 Answers

You can just store the .value_counts() method output and then just filter it:

>>> counts = df.groupby('A')['B'].value_counts()
>>> counts[counts >= 2]
A    B  
bar  no     4
foo  yes    3
     no     2
Name: B, dtype: int64

If you want to get your desired output, you can call .reset_index() method and rename the new column:

>>> counts[counts >= 2].reset_index(name='count') 
     A    B  count
0  bar   no      4
1  foo  yes      3
2  foo   no      2
like image 90
EdChum Avatar answered Oct 22 '25 16:10

EdChum


This can be down with one line with .loc

>>> df.groupby('A')['B'].value_counts().loc[lambda x: x > 1].reset_index(name='count')
     A    B  count
0  bar   no      4
1  foo  yes      3
2  foo   no      2
like image 31
BENY Avatar answered Oct 22 '25 16:10

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!