使用索引 + 行匹配从数据帧中删除重复项

Removing duplicates from dataframe with index + row matching

提问人:dailyglen 提问时间:5/27/2012 最后编辑:piRSquareddailyglen 更新时间:1/5/2017 访问量:1197

问:

我有两个 s,我想将它们连接在一起,以便我删除了重复项的外部连接。我的问题是在查找重复项时忽略索引。如果索引不同,则不应重复。如果行索引和列是重复的,如何删除重复项?我唯一能想到的就是使用然后创建一个新的 DataFrame(效率非常低)。pandasDataFrame.drop_duplicates()df.to_dict()

更新:

根据要求,这是我的数据示例:

from pandas import *
index1 = ['2012-05-2' + str(i) for i in range(0,6)]
data1 = {'rate': range(0,6)}
a = DataFrame(data1, index1)

index2 = ['2012-05-2' + str(i) for i in range(3,9)]
data2 = {'rate': range(3,9)}
b = DataFrame(data2, index2)

幽谷

Python 熊猫

评论

1赞 Wes McKinney 5/28/2012
你有没有检查过是否是你想要的?否则,您能否举例说明您的数据以及您期望/希望的结果是什么?a.combine_first(b)
0赞 lbolla 5/28/2012
你试过吗?pandas.merge(A, B, method="outer")

答:

1赞 dailyglen 5/29/2012 #1

溶液:

a.combine_first(b)

谢谢韦斯。