I want to calculate conditional probabilites of ratings(\'A\',\'B\',\'C\') in ratings column.
company model rating type
0 ford mustang
You need add reindex for add 0 values for missing pairs:
mux = pd.MultiIndex.from_product([df['rating'].unique(), df['type'].unique()])
s = (df.groupby(['rating', 'type']).count() / df.groupby('rating').count())['model']
s = s.reindex(mux, fill_value=0)
print (s)
A coupe 0.500000
sedan 0.500000
B coupe 0.333333
sedan 0.666667
C coupe 0.000000
sedan 1.000000
Name: model, dtype: float64
And another solution, thanks Zero:
s.unstack(fill_value=0).stack()