简体   繁体   English

在熊猫列中查找唯一值,其中每一行都有多个值

[英]Finding unique values in pandas column where each row has multiple values

I have following column in a dataframe which contains colors seprated by | 我在dataframe框中有以下列,其中包含用|分隔的颜色

df = pd.DataFrame({'x': ['RED|BROWN|YELLOW', 'WHITE|BLACK|YELLOW|GREEN', 'BLUE|RED|PINK']})

I want to find all unique colors from the column. 我想从该列中找到所有唯一的颜色。

Expected Output : 预期产量

{'YELLOW', 'BLACK', 'RED', 'BLUE', 'BROWN', 'GREEN', 'WHITE', 'PINK'}

I don't mind if it is list or set . 我不在乎它是list还是set

What I tried : 我试过了

df['x'] = df['x'].apply(lambda x: x.split("|"))

colors = []
for idx, row in df.iterrows():
    colors.extend(row['x'])

print(set(colors))

Which is working fine but I am looking for more efficient solution as I have large dataset. 哪个工作正常,但由于数据集很大,我正在寻找更有效的解决方案。

set(df.loc[:, 'x'].str.split('|', expand=True).values.ravel())

要么

set(df.loc[:, 'x'].str.split('|', expand=True).values.ravel()) - set([None])
list(df.x.str.split('|', expand=True).stack().reset_index(name='x').drop_duplicates('x')['x'])

产量

['RED', 'BROWN', 'YELLOW', 'WHITE', 'BLACK', 'GREEN', 'BLUE', 'PINK']

Use itertools (which is arguably the fastest in flattening lists ) with set; 使用带有set的itertools (可以说是最快的扁平化列表);

import itertools
set(itertools.chain.from_iterable(df.x.str.split('|')))

Output: 输出:

{'BLACK', 'BLUE', 'BROWN', 'GREEN', 'PINK', 'RED', 'WHITE', 'YELLOW'}

Another possible solution with functools which is almost as fast as itertools: functools另一种可能的解决方案几乎与itertools一样快:

import functools
import operator
set(functools.reduce(operator.iadd, df.x.str.split('|'), []))

Note you can also use sum() which seems readable but not quite as fast. 注意,您也可以使用sum() ,它看起来可读但不那么快。

You can also do set(df['x'].str.split('|').values.sum()) 您也可以set(df['x'].str.split('|').values.sum())

This will also remove None form the output 这也会从输出中删除None

{'YELLOW', 'RED', 'WHITE', 'BROWN', 'GREEN', 'PINK', 'BLUE', 'BLACK'}

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

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