我有一个带日期索引的数据框。有几个日期不知何故失踪了。我将这称为数据帧A
。我有另一个数据框,其中包含相关日期。我会称之为数据帧B
。
我想合并两个数据帧:
保留所有索引A
并加入B
,但我不希望B
该共享中的任何行与索引A
。也就是说,我只想从A
返回的行中丢失B
。
这最容易实现的是什么?
注意:
对于我拥有的数据数据库,这种行为是正确的。我将做大约400次。








需要Index.difference:
B.loc[B.index.difference(A.index)]
编辑:
A = pd.DataFrame({'A':range(10)}, index=pd.date_range('2019-02-01', periods=10))
B = pd.DataFrame({'A':range(10, 20)}, index=pd.date_range('2019-01-27', periods=10))
df = pd.concat([A, B.loc[B.index.difference(A.index)]]).sort_index()
print (df)
A
2019-01-27 10
2019-01-28 11
2019-01-29 12
2019-01-30 13
2019-01-31 14
2019-02-01 0
2019-02-02 1
2019-02-03 2
2019-02-04 3
2019-02-05 4
2019-02-06 5
2019-02-07 6
2019-02-08 7
2019-02-09 8
2019-02-10 9
df1= pd.concat([A, B])
df1 = df1[~df1.index.duplicated()].sort_index()
print (df1)
A
2019-01-27 10
2019-01-28 11
2019-01-29 12
2019-01-30 13
2019-01-31 14
2019-02-01 0
2019-02-02 1
2019-02-03 2
2019-02-04 3
2019-02-05 4
2019-02-06 5
2019-02-07 6
2019-02-08 7
2019-02-09 8
2019-02-10 9