pandas 在由列表组成的元素上放置重复项 [英] Pandas drop duplicates on elements made of lists
本文介绍了 pandas 在由列表组成的元素上放置重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我的数据框是:
df = pandas.DataFrame([[[1,0]],[[0,0]],[[1,0]]])
产生:
0
0 [1, 0]
1 [0, 0]
2 [1, 0]
我想删除重复项,如果我写的话,只会得到元素[1,0]和[0,0]:
I want to drop duplicates, and only get elements [1,0] and [0,0], if I write:
df.drop_duplicates()
我收到以下错误:TypeError:无法散列的类型:'list'
I get the following error: TypeError: unhashable type: 'list'
如何调用drop_duplicates()?
How can I call drop_duplicates()?
更多一般信息:
df = pandas.DataFrame([[[1,0],"a"],[[0,0],"b"],[[1,0],"c"]], columns=["list", "letter"])
我想调用df ["list"].drop_duplicates(),所以drop_duplicates适用于Series而不是数据框吗?
And I want to call df["list"].drop_duplicates(), so drop_duplicates applies to a Series and not a dataframe?
推荐答案
您可以使用numpy.unique()
函数:
>>> df = pandas.DataFrame([[[1,0]],[[0,0]],[[1,0]]])
>>> pandas.DataFrame(np.unique(df), columns=df.columns)
0
0 [0, 0]
1 [1, 0]
如果您要保留订单结帐: numpy.unique保留了订单
If you want to preserve the order checkout: numpy.unique with order preserved
这篇关于 pandas 在由列表组成的元素上放置重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文