Let's assume we have the following data frame df:
df = pd.DataFrame({'food' : ['spam', 'ham', 'eggs', 'ham', 'ham', 'eggs', 'milk'],
'sales' : [10, 15, 12, 5, 14, 3, 8]})
I'd like to pivot this data to show the sum of sales by food, but only if sales is greater than 12. The resulting pivot table would look as follows:
Unfiltered df:
food sum(sales)
spam 10
ham 34
eggs 15
milk 8
Filtered df:
food sum(sales)
ham 34
eggs 15
I can use groupby() as follows:
df_new.groupby(['food'])['sales'].agg('sum') > 12
But, this only gives me the boolean and not the filtered df.
Is this possible to filter a column "on the fly" when using the pd.pivot_table() function? (i.e. without pre-filtering the df)