Python,从列表的DataFrame列中删除重复值
我有一个包含列表的DataFrame列,我想从各个列表中删除重复值。
d = {'colA': [['UVB', 'NER', 'GGR', 'NER'], ['KO'], ['ERK1', 'ERK1', 'ERK2'], []]}
df = pd.DataFrame(data=d)
我想从列表中删除重复的“ ner”和“ erk1”。
我尝试过:
df['colA'] = set(tuple(df['colA']))
我收到错误消息: TypeError:不可用的类型:“列表”
I've got a dataframe column containing lists, and I want to remove duplicate values from the individual lists.
d = {'colA': [['UVB', 'NER', 'GGR', 'NER'], ['KO'], ['ERK1', 'ERK1', 'ERK2'], []]}
df = pd.DataFrame(data=d)
I want to remove the duplicate 'NER' and 'ERK1' from the lists.
I've tried:
df['colA'] = set(tuple(df['colA']))
I get the error message:
TypeError: unhashable type: 'list'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
apply()
pandas函数的方法从列表中删除重复值,如下所示。You can remove duplicates values from the list using
apply()
method of pandas function as follows.问题是您有列表的元组,这就是为什么设置命令不起作用。您应该在整个元组上迭代。
ans = tuple(df ['cola'])在范围(len(ans))df ['cola']中。
problem is that you have a tuple of lists, thats why set command doesnt work. You should iterate over entire tuple.
ans = tuple(df['colA']) for i in range(len(ans)) df['colA'].iloc[i]=set(ans[i])