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

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

    first, convert into a pandas dataframe. by doing so, you can take advantage of pandas' groupby methods.

    collection = {"company": ["ford", "chevy", "ford", "ford", "ford", "toyota"],
                  "model": ["mustang", "camaro", "fiesta", "focus", "taurus", "camry"],
                  "rating": ["A", "B", "C", "A", "B", "B"],
                  "type": ["coupe", "coupe", "sedan", "sedan", "sedan", "sedan"]}
    
    df = pd.DataFrame(collection)
    

    then, groupby based on events (ie rating).

    df_s = df.groupby('rating')['type'].value_counts() / df.groupby('rating')['type'].count()
    df_f = df_s.reset_index(name='cpt')
    df_f.head()  # your conditional probability table
    

提交回复
热议问题