What i want is including zero counts categories while generating frequencies for categorical variables
example:
df = pd.DataFrame({
'col1': ['a', 'c', 'a', 'e'],
'col2': ['x', 'y', 'y', 'z'],
})
col1 col2
a x
c y
a y
e z
what I want is to generate a frequency table with counts and percentages including zero counts categories
results
Counts Cercentage
a 2 50.0%
b 0 0.0%
c 1 25.0%
d 0 0.0%
e 1 25.0%
What I have done is generating the frequency table with counts and percentages, but I need to include also the zero counts categories like b and d as illustrated above.
Here's what I have tried:
pd.concat([df.col1.value_counts(dropna=False),
df.col1.value_counts(normalize=True,
dropna=False).mul(100).round(1).astype(str) + '%'],
axis=1,
keys=('Counts','Percentage'))
any help please