How to calculate conditional probability of values in dataframe pandas-python?

前端 未结 4 1916
误落风尘
误落风尘 2020-12-13 16:32

I want to calculate conditional probabilites of ratings(\'A\',\'B\',\'C\') in ratings column.

    company     model    rating   type
0   ford       mustang          


        
4条回答
  •  执念已碎
    2020-12-13 17:00

    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
    

提交回复
热议问题