0
我想弄清楚如何顯示2熊貓數據幀之間的差異。我快到了,但似乎無法弄清楚如何顯示包含差異的行的其他數據。熊貓在數據幀中的差異
這是我到目前爲止有:
數據幀一比較,以數據幀B:
數據框答:
Date ID_1 ID_2 Value
1-Jan 1 1 5
2-Jan 1 2 6
3-Jan 1 3 4
4-Jan 1 4 2
5-Jan 1 5 8
數據幀B:
Date ID_1 ID_2 Value
1-Jan 1 1 5
2-Jan 1 2 6
3-Jan 1 3 4
4-Jan 1 4 2
5-Jan 1 5 55
電流輸出:
Date Column From To
5-Jan Value 8 55
所需的輸出:
Date ID_1 ID_2 From To
5-Jan 1 5 8 55
目前代碼:
#stack column(s) where dataframes are not equal
ne_stacked = (df1 != df2).stack()
#create new dataframe from ne_stacked
changed = ne_stacked[ne_stacked]
#change column names
changed.index.names = ['Date', 'Column']
#create array where dataframes are not equal
diff_loc = np.where(df1 != df2)
#create 'from' column
changed_from = df1.values[diff_loc]
#create 'to' column
changed_to = df2.values[diff_loc]
#create a summary dataframe
final = pd.DataFrame({'From': changed_from, 'To': changed_to}, index=changed.index)
print final