How to make a pandas crosstab with percentages?

前端 未结 6 663
时光说笑
时光说笑 2021-01-30 01:44

Given a dataframe with different categorical variables, how do I return a cross-tabulation with percentages instead of frequencies?

df = pd.DataFrame({\'A\' : [\         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-30 01:56

    pd.crosstab(df.A, df.B).apply(lambda r: r/r.sum(), axis=1)
    

    Basically you just have the function that does row/row.sum(), and you use apply with axis=1 to apply it by row.

    (If doing this in Python 2, you should use from __future__ import division to make sure division always returns a float.)

提交回复
热议问题