I want to calculate conditional probabilites of ratings(\'A\',\'B\',\'C\') in ratings column.
company model rating type
0 ford mustang
You can use .groupby()
and the built-in .div():
rating_probs = df.groupby('rating').size().div(len(df))
rating
A 0.333333
B 0.500000
C 0.166667
and the conditional probs:
df.groupby(['type', 'rating']).size().div(len(df)).div(rating_probs, axis=0, level='rating')
coupe A 0.500000
B 0.333333
sedan A 0.500000
B 0.666667
C 1.000000