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

前端 未结 4 1948
误落风尘
误落风尘 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:13

    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()
    

提交回复
热议问题