我正在考虑为大型数据帧编写更改日志的最有效方法。我有数千个拥有100万行和20列的数据帧,因此效率至关重要。我有几个解决方案来检查两个数据帧之间的差异,但我无法找出记录更改的最佳方法。骨架算法是:
我相信我坚持第3步和第4步。我想我需要将屏蔽数据帧合并到df3数据帧,然后进行某种操作来连接字符串。有人有什么想法吗?
我在循环内部尝试了以下更改df1和df2,但获得了错误的结果:
import pandas as pd
import numpy as np
df1=pd.DataFrame({'Col1' : ['blue', 2, 3, 4], 'Col2' : [90, 99, 3, 97], 'Col3' : [11, 12, 13, 14]})
df2=pd.DataFrame({'Col1' : ['blue', 2, 6], 'Col2' : [90, 99, 99], 'Col3' : [11, 12, 13]})
df3=df1
mask=df2.ne(df1)
#This is where trouble begins...df3 is overwritten so the log is only for this one comparison...
#How do I merge the data into df3 so that df3 has the string from df3 and the additions from df2.ne(df1)
df3=(df3.astype(str)[mask] + ' changed to: ' + df2.astype(str)[mask]).fillna(df2.astype(str)).fillna('Removed')
谢谢!