python equivalent of R table

前端 未结 7 1592
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 19:27

I have a list

[[12, 6], [12, 0], [0, 6], [12, 0], [12, 0], [6, 0], [12, 6], [0, 6], [12, 0], [0, 6], [0, 6], [12, 0], [0, 6], [6, 0], [6, 0], [12, 0], [6, 0], [         


        
7条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 20:08

    IMHO, pandas offers a better solution for this "tabulation" problem:

    One dimension:

    my_tab = pd.crosstab(index = df["feature_you_r_interested_in"],
                                  columns="count")
    

    Proportion count:

    my_tab/my_tab.sum()
    

    Two-dimensions (with totals):

    cross = pd.crosstab(index=df["feat1"], 
                                 columns=df["feat2"],
                                 margins=True)
    
    cross
    

    Also, as mentioned by other coleagues, pandas value_counts method could be all you need. It is so good that you can have the counts as percentages if you want:

    df['your feature'].value_counts(normalize=True)
    

    I'm very grateful for this blog:

    http://hamelg.blogspot.com.br/2015/11/python-for-data-analysis-part-19_17.html

提交回复
热议问题