本文介绍了删除重复的 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用drop_duplicates从数据框中删除重复的列时,我收到一条错误消息.
I am getting an error message when using drop_duplicates to drop duplicate columns from my dataframe.
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
下面是一个最小的示例(请注意,我这里没有重复的列名,因为那样的话就不会添加该列,所以我的var1在我的实际数据帧中将被称为var0)
Below is a min example (notice that I don't have duplicate column names here, since that column won't be added then, so I var1 would be called var0 in my actual dataframe)
dict1 = [{'var0': 0, 'var1': 0, 'var2': 2},
{'var0': 0, 'var1': 0, 'var2': 4},
{'var0': 0, 'var1': 0, 'var2': 8},
{'var0':0, 'var1': 0, 'var2': 12},]
df = pd.DataFrame(dict1, index=['s1', 's2','s1','s2'])
df.T.drop_duplicates().T
推荐答案
问题出在索引上,当您转置DataFrame时,会得到重复的列名,这些列名将其弄乱了.见下文
The problem is with your indexing, when you transpose your DataFrame you will get duplicate column names which are messing it up. See below
dict1 = [{'var0': 0, 'var1': 0, 'var2': 2},
{'var0': 0, 'var1': 0, 'var2': 4},
{'var0': 0, 'var1': 0, 'var2': 8},
{'var0':0, 'var1': 0, 'var2': 12},]
df = pd.DataFrame(dict1, index=['s1', 's2','s1','s2'])
df.reset_index().T.drop_duplicates().T.set_index('index')
这篇关于删除重复的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!