【发布时间】:2019-09-24 18:38:09
【问题描述】:
我想比较两个 Pandas DataFrame 并获取差异的索引。
import numpy as np
import pandas as pd
rng = pd.date_range('2019-03-04', periods=5)
cols = ['A', 'B', 'C', 'D']
df1 = pd.DataFrame(np.arange(20).reshape(5, 4), index=rng, columns=cols)
df2 = pd.DataFrame(np.arange(20).reshape(5, 4), index=rng, columns=cols)
df2.iloc[2, 2] = 100
df2.iloc[3, 1] = 50
df1.equals(df2) # OK, good to know, but where is the difference?
df1 == df2 # Nice, too. But I'm interested in the indices!
# I need a list containing [(2,2), (3,1)]. Even more intuitive would be something like [('2019-03-06', 'C'), ('2019-03-07', 'B')]
编辑:我不一定需要一个列表,但需要一些东西来识别索引。也就是说,如果有一种简单直观的方法可以在没有列表的情况下解决该问题,那很好。但是,列表也可以。
【问题讨论】: