找出具有相同形状的两个DataFrame之间的差异
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找出具有相同形状的两个DataFrame之间的差异相关的知识,希望对你有一定的参考价值。
我试图找到两个具有行数的excel文件之间的差异。我首先想要在两列上对两个工作簿进行排序,然后输出具有差异的第三个文件。我无法正确导出差异文件。
任何帮助都非常感谢!!!提前致谢!
将pandas导入为pd
df1 = pd.DataFrame({
'ID' : ['3', '3', '55','55', '66', '66'],
'date' : [20180102, 20180103, 20180104, 20180105, 20180106, 20180107],
'age': [0, 1, 9, 4, 2, 3],
})
df2 = pd.DataFrame({
'ID' : ['3', '55', '3','66', '55', '66'],
'date' : [20180103, 20180104, 20180102, 20180106, 20180105, 20180107],
'age': [0, 1, 9, 9, 8, 7],
})
df3 = df1.sort_values(by= ['ID', 'date'] , ascending=False)
df4 = df2.sort_values(by= ['ID', 'date'] , ascending=False)
dfDiff = df3.copy()
for row in range(dfDiff.shape[0]):
for col in range(dfDiff.shape[1]):
value_old = df3.iloc[row,col]
value_new = df4.iloc[row,col]
if value_old == value_new:
dfDiff.iloc[row,col] = df4.iloc[row,col]
else:
dfDiff.iloc[row,col] = ('{}->{}').format(value_old,value_new)
writer = pd.ExcelWriter('diff', engine='xlsxwriter')
dfDiff.to_excel(writer, sheet_name='DIFF', index= False)
workbook = writer.book
worksheet = writer.sheets['DIFF']
worksheet.hide_gridlines(2)
writer.save()
答案
我认为你只是在文件路径的末尾错过了.xlsx
df1 = pd.DataFrame({
'ID' : ['3', '3', '55','55', '66', '66'],
'date' : [20180102, 20180103, 20180104, 20180105, 20180106, 20180107],
'age': [0, 1, 9, 4, 2, 3],
})
df2 = pd.DataFrame({
'ID' : ['3', '55', '3','66', '55', '66'],
'date' : [20180103, 20180104, 20180102, 20180106, 20180105, 20180107],
'age': [0, 1, 9, 9, 8, 7],
})
df3 = df1.sort_values(by= ['ID', 'date'] , ascending=False)
df4 = df2.sort_values(by= ['ID', 'date'] , ascending=False)
dfDiff = df3.copy()
for row in range(dfDiff.shape[0]):
for col in range(dfDiff.shape[1]):
value_old = df3.iloc[row,col]
value_new = df4.iloc[row,col]
if value_old == value_new:
dfDiff.iloc[row,col] = df4.iloc[row,col]
else:
dfDiff.iloc[row,col] = ('{}->{}').format(value_old,value_new)
# added `.xlsx' to path here
writer = pd.ExcelWriter('diff.xlsx', engine='xlsxwriter')
dfDiff.to_excel(writer, sheet_name='DIFF', index= False)
workbook = writer.book
worksheet = writer.sheets['DIFF']
worksheet.hide_gridlines(2)
writer.save()
以上是关于找出具有相同形状的两个DataFrame之间的差异的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server 中是不是有一种方法可以显示两个具有相同布局并共享一些公共数据的表之间的字段差异
Pyspark DataFrame:查找两个 DataFrame 之间的差异(值和列名)
基于pyspark中仅一列的两个DataFrame之间的差异[重复]