Here's what the dataset I'm working on looks like:
| Type | SubType | Municipality |
|---|---|---|
| Social Media | New Castle | |
| Onground | Campus | Monroe |
| Onground | Cafe | Kutlski |
| Social Media | New Castle | |
| Social Media | Tiktok | San Andreas |
| Social Media | New Castle | |
| Social Media | 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 | New Castle | 2 | |
| San Andreas | 1 | ||
| New castle | 1 | ||
| TikTok | San Andreas | 1 |
But instead, this is my result:
| Type | SubType | Municipality | |
|---|---|---|---|
| True | New Castle | 2 | |
| San Andreas | 1 | ||
| New Castle | 1 | ||
| Titkok | San Andreas | 1 | |
| False | Onground | Cafe | 1 |
| Campus | 1 | ||
| and so on... | .... | .... | ... |
CodePudding user response:
Just filter first and group only by subtype. df.query('Type == `Social Media`').groupby('SubType')['Municipality'].value_counts()
CodePudding user response:
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
