I am making a pivot table using pandas. If I set aggfunc=sum
or aggfunc=count
on a column of boolean values, it works fine provided there's at least one True
in the column. E.g. [True, False, True, True, False]
would return 3. However, if all the values are False
, then the pivot table outputs False
instead of 0. No matter what, I can't get around it. The only way I can circumvent it is to define a function follows:
def f(x):
mySum = sum(x)
return "0" if mySum == 0 else mySum
and then set aggfunc=lambda x: f(x)
. While that works visually, it still disturbs me that outputing a string
is the only way I can get the 0 to stick. If I cast it as an int
, or try to return 0.0, or do anything that's numeric at all, False
is always the result.
Why is this, and how do I get the pivot table to actually give me 0 in this case (by only modifying the aggfunc
, not the dataframe itself)?