第一部分类似于君士坦丁,您可以获取其中的行为空的布尔值*:
In [21]: ne = (df1 != df2).any(1)In [22]: neOut[22]:0 False1 True2 Truedtype: bool
然后,我们可以查看哪些条目已更改:
In [23]: ne_stacked = (df1 != df2).stack()In [24]: changed = ne_stacked[ne_stacked]In [25]: changed.index.names = ['id', 'col']In [26]: changedOut[26]:id col1 score True2 isEnrolled True Comment Truedtype: bool
在这里,第一个条目是索引,第二个条目是已更改的列。
In [27]: difference_locations = np.where(df1 != df2)In [28]: changed_from = df1.values[difference_locations]In [29]: changed_to = df2.values[difference_locations]In [30]: pd.Dataframe({'from': changed_from, 'to': changed_to}, index=changed.index)Out[30]: fromtoid col1 score 1.11 1.212 isEnrolled True False Comment None On vacation*注:这是非常重要的
df1,并
df2在这里分享相同的索引。为了克服这种歧义,您可以确保仅使用来查看共享标签
df1.index & df2.index,但我想将其保留为练习。



