简体   繁体   English

从列表格式的数据框列中删除重复项

[英]Remove dupes from a data frame column that is a list format

I have ton of duplicate values in a data frame column by row. 我在数据帧中逐行有大量重复值。 Below is some sample, I looked at other stack overflow question, but I can only find the answer for the list not for the data frame issue dupes. 下面是一些示例,我查看了其他堆栈溢出问题,但是我只能找到列表的答案,而不是数据帧问题重复的答案。 When I pass values in a list, I am able to remove duplicate values however, when I pass it like a data frame it is giving errors: TypeError: unhashable type: 'list' 当我在列表中传递值时,我可以删除重复的值,但是,当我像数据框一样传递它时,它会给出错误: TypeError: unhashable type: 'list'

What am I doing wrong here? 我在这里做错了什么?

import pandas as pd 
d = {'col1': ['apples are delicious,apples are delicious,apples', 'apples'], 'col2': ['mangoes','oranges']}
df = pd.DataFrame(data=d)
df['col1'] = set(df['col1'].str.split(",")) #error tried list(set()) as well.
df['col2'] = df['col2'].str.split(",") #converting to list
print(df)

final output should remove dupes like this: 最终输出应删除重复项,如下所示:

col1                                         co2
['apples are delicious','apples']            ['mangoes']
['apples']                                   ['oranges']

You are using set on an entire series, whereas you need to apply set to each element in the series. 您正在整个系列上使用set ,而您需要将set应用于set中的每个元素 For this, you can use pd.Series.map : 为此,您可以使用pd.Series.map

df['col1'] = df['col1'].str.split(',').map(set)

print(df)

                             col1       col2
0  {apples are delicious, apples}  [mangoes]
1                        {apples}  [oranges]

Your error derives from the fact you can't have a set of lists since lists are not hashable. 您的错误源于以下事实:由于列表不可哈希,因此您无法拥有一set列表。

If you really need a series of lists as the result, you can use the same method again, ie df['col1'].str.split(',').map(set).map(list) . 如果确实需要一系列列表作为结果,则可以再次使用相同的方法,即df['col1'].str.split(',').map(set).map(list) But note that you should assume no ordering as set is an unordered collection. 但是请注意,您不应该假设set中的任何排序都是无序集合。

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

相关问题 如果 substring 在数据框列的列表中,则从字符串中删除 substring - Remove substring from string if substring in list in data frame column 我们如何计算数据框列中的重复数据并将结果分配给同一数据框中的新列? - How can we count dupes in a column of a data frame and assign the results to a new column in the same data frame? 在Python中从数据框中删除列 - Remove Column from the data frame in Python 以特定格式将数据框的列格式从Str更改为Date - Changing the format of a column of Data frame from Str to Date in specific format Python 删除由数据框列组成的列表列表中的外部引号 - Python remove outer quotes in a list of lists made from a data frame column 如果 substring 在数据框列的列表中,则从字符串中删除多字 substring - Remove multi-word substring from string if substring in list in data frame column 从不在列表中的每行数据框中的列中删除字符串 - Remove Strings from Column in data frame per row that aren't in a list Pandas从列表中创建数据框列 - Pandas Create Data Frame Column from List 从数据框列中的列表中查找元素(列的类型是列表) - Finding an element from a list in column of data frame (the type of column is a list) 从数据框列python中删除非日期值 - Remove non date values from data-frame column python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM