简体   繁体   中英

Pandas groupby 2 coluns/conditions then value_counts() by another column?

Here's what the dataset I'm working on looks like:

Type SubType Municipality
Social Media Facebook New Castle
Onground Campus Monroe
Onground Cafe Kutlski
Social Media Instagram New Castle
Social Media Tiktok San Andreas
Social Media Facebook New Castle
Social Media Facebook San Andreas

I want to group it by Type and SubType then further filter it by Municipality and then value_counts() it.

Here's what I've tried:

ab = df.groupby([df['Type'] == 'Social Media', 
                     df['SubType']])
ab['Municipality'].value_counts()

I almost got what I want only that it shows everything, not just the result of the condition (under the Type column, it has 'true' and false' section.

This is the result I'm looking for:

Type SubType Municipality
Social Media Facebook New Castle 2
San Andreas 1
Instagram New castle 1
TikTok San Andreas 1

But instead, this is my result:

Type SubType Municipality
True Facebook New Castle 2
San Andreas 1
Instagram New Castle 1
Titkok San Andreas 1
False Onground Cafe 1
Campus 1
and so on... .... .... ...

Just filter first and group only by subtype. df.query('Type == `Social Media`').groupby('SubType')['Municipality'].value_counts()

I think value_counts is what you are looking for

df.value_counts(['Type','SubType','Municipality'])
Out[169]: 
Type          SubType    Municipality
Social Media  Facebook   New Castle      2
Onground      Cafe       Kutlski         1
              Campus     Monroe          1
Social Media  Facebook   San Andreas     1
              Instagram  New Castle      1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM