3 回答
TA貢獻1799條經驗 獲得超9個贊
第一部分類似于君士坦丁,您可以獲取其中的行為空的布爾值*:
In [21]: ne = (df1 != df2).any(1)
In [22]: ne
Out[22]:
0 False
1 True
2 True
dtype: bool
然后,我們可以查看哪些條目已更改:
In [23]: ne_stacked = (df1 != df2).stack()
In [24]: changed = ne_stacked[ne_stacked]
In [25]: changed.index.names = ['id', 'col']
In [26]: changed
Out[26]:
id col
1 score True
2 isEnrolled True
Comment True
dtype: 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]:
from to
id col
1 score 1.11 1.21
2 isEnrolled True False
Comment None On vacation
*注:這是非常重要的df1,并df2在這里分享相同的索引。為了克服這種歧義,您可以確保僅使用來查看共享標簽df1.index & df2.index,但我想將其保留為練習。
添加回答
舉報
