【问题标题】:Counting unique values in a pandas dataframe column consisting mixed values of strings and list of strings计算由字符串的混合值和字符串列表组成的 pandas 数据框列中的唯一值
【发布时间】:2020-01-27 12:18:20
【问题描述】:

我想计算数据框特定列中的唯一值总数。该列如下所示:

df['column']=([one, two], three, four, [one,five], three])

期望的输出是:5

我试过df['column'].nunique(),但由于列中有列表,它不起作用。

我在循环中尝试了 Counter 方法,但我无法获得唯一计数的总数,而是获得了每行的唯一计数。

有人知道怎么解决吗?

【问题讨论】:

    标签: python pandas unique


    【解决方案1】:

    这是一种方法:

    df['column'].explode().nunique()
    
    5
    

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2015-07-25
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2021-04-27
      • 2019-05-30
      • 2023-03-02
      相关资源
      最近更新 更多