【问题标题】:DataFrame values frequency [duplicate]DataFrame值频率[重复]
【发布时间】:2021-03-25 21:06:45
【问题描述】:

我有一个 DataFrame,我想在整个帧中查找值频率。

    a   b
0   5   7
1   7   8
2   5   7

结果应该是这样的:

 5 2
 7 3
 8 1

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    DataFrame.stackSeries.value_countsSeries.sort_index 一起使用:

    s = df.stack().value_counts().sort_index()
    

    DataFrame.melt:

    s = df.melt()['value'].value_counts().sort_index()
    print (s)
    5    2
    7    3
    8    1
    Name: value, dtype: int64
    

    【讨论】:

      【解决方案2】:

      一种简单的方法是使用pd.Series 来查找唯一计数:

      import pandas as pd
      
      # creating the series
      s = pd.Series(data = [5,10,9,8,8,4,5,9,10,0,1])  
      # finding the unique count
      print(s.value_counts())
      

      输出:

      10    2
      9     2
      8     2
      5     2
      4     1
      1     1
      0     1
      

      【讨论】:

        猜你喜欢
        • 2016-06-30
        • 1970-01-01
        • 2021-02-08
        • 2021-05-09
        • 2018-02-14
        • 2016-10-21
        • 2020-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多