代码之家  ›  专栏  ›  技术社区  ›  MarkS

列中列表中元素的值计数

  •  1
  • MarkS  · 技术社区  · 2 年前

    我有一个列,其中包含各种大小的列表,但项目数量有限。

    print(df['channels'].value_counts(), '\n')
    

    输出:

    [web, email, mobile, social]    77733
    [web, email, mobile]            43730
    [email, mobile, social]         32367
    [web, email]                    13751
    

    所以我想要的是网络、电子邮件、移动和社交的总次数。

    这些应该是:

    web =    77733 + 43730 + 13751            135,214
    email =  77733 + 43730 + 13751 + 32367    167,581
    mobile = 77733 + 43730 + 32367            153,830
    social = 77733 + 32367                    110,100
    

    我尝试了以下两种方法:

    sum_channels_items = pd.Series([x for item in df['channels'] for x in item]).value_counts()
    print(sum_channels_items)
    
    from itertools import chain
    test = pd.Series(list(chain.from_iterable(df['channels']))).value_counts()
    print(test)
    

    这两种方法都会失败,并出现相同的错误(仅显示第二个错误)。

    Traceback (most recent call last):
      File "C:/Users/Mark/PycharmProjects/main/main.py", line 416, in <module>
        test = pd.Series(list(chain.from_iterable(df['channels']))).value_counts()
    TypeError: 'float' object is not iterable
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   enke    2 年前

    一个选择是 explode ,然后计算值:

    out = df['channels'].explode().value_counts()
    

    另一个可能是使用 collections.Counter 。请注意,您的错误表明列中缺少值,因此可以先删除它们:

    from itertools import chain
    from collections import Counter
    out = pd.Series(Counter(chain.from_iterable(df['channels'].dropna())))