Pandas: Counting unique values in a dataframe

前端 未结 2 693
情歌与酒
情歌与酒 2020-12-16 03:37

We have a DataFrame that looks like this:

> df.ix[:2,:10]
    0   1   2   3   4   5   6   7   8   9   10
0   NaN NaN NaN NaN  6   5  NaN NaN  4  NaN  5
1          


        
2条回答
  •  一个人的身影
    2020-12-16 04:31

    Not enough rep to comment, but Andy's answer:

    pd.value_counts(d.values.ravel()) 
    

    is what I have used personally, and seems to me to be by far the most versatile and easily-readable solution. Another advantage is that it is easy to use a subset of the columns:

    pd.value_counts(d[[1,3,4,6,7]].values.ravel()) 
    

    or

    pd.value_counts(d[["col_title1","col_title2"]].values.ravel()) 
    

    Is there any disadvantage to this approach, or any particular reason you want to use stack and groupby?

提交回复
热议问题