简体   繁体   English

计算由字符串和字符串列表的混合值组成的熊猫数据框列中的唯一值

[英]Counting unique values in a pandas dataframe column consisting mixed values of strings and list of strings

I would like to count the total unique values in a specific column of a data frame.我想计算数据框特定列中的总唯一值。 The column looks like the following:该列如下所示:

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

The desired output would be: 5所需的输出是:5

I tried df['column'].nunique() but since there are lists in the column it didn't work.我试过df['column'].nunique()但由于列中有列表它不起作用。

I tried the Counter method in a loop but I was not able to get the total unique count but instead I got unique count per line.我在循环中尝试了Counter方法,但我无法获得总唯一计数,而是获得了每行的唯一计数。

Does anybody know how to solve this?有谁知道如何解决这个问题?

Here's a way to do:这是一种方法:

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

5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM