'Filter multi index pivot table based on value count
My code for the pivot table is:
games_df.pivot_table(index=['Name', 'Platform'], values='total_sale', aggfunc='sum')
I get the following pivot table:
total_sale
Name Platform
beyblade burst 3DS 0.03
fire emblem fates 3DS 1.67
frozen: olaf's quest 3DS 0.59
DS 0.51
haikyu!! cross team match! 3DS 0.04
... ...
zumba fitness: world party Wii 0.23
XOne 0.24
zwei!! PSP 0.02
zyuden sentai kyoryuger: game de gaburincho!! 3DS 0.05
¡shin chan flipa en colores! DS 0.14
I want to compare the total_sale of the same game between different platforms and this is why I want to filter out games that only have one value under the Platform column. I want to be left out with df that have games that were released on more then one platform. Is it possible to do such filtering when calling pivot_table?
Solution 1:[1]
I was able to solve it:
pivot=games_df.pivot_table(index=['Name', 'Platform'], values='total_sale', aggfunc='sum')
pivot.groupby('Name').filter(lambda x: len(x) > 1)
total_sale
Name Platform
frozen: olaf's quest 3DS 0.59
DS 0.51
007: quantum of solace DS 0.13
PC 0.02
PS2 0.43
... ...
zumba fitness X360 2.37
zumba fitness core Wii 0.07
X360 0.05
zumba fitness: world party Wii 0.23
XOne 0.24
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oris |
