I'm creating multiple pivot tables using a Categorical
dtype then merging them into one big pivot table / dataframe.
However, in some cases I get NaN
when I perform the merge, and when I try to fillna(0)
, I get the following error: ValueError: fill value must be in categories
pv1 = pd.PivotTable(df, index='Category', values='Sales', aggfunc='sum')
pv2 = pd.PivotTable(df, index='Category', values='Quantity', aggfunc='sum')
chart = pv1.merge(pv2, on='Category', how='outer').fillna(0)
Actual Output:
Category Sales Quantity
Boxes 100 NaN
Staples 20 10
Paper NaN 20
Desired Output:
Category Sales Quantity
Boxes 100 0
Staples 20 10
Paper 0 20