我有2个表,其中有3行3列:
Table 1 Table 2
1 7 9 1 4 7
2 4 11 12 -4 -1
7 3 2 2 4 11
我想将这两个表合并到一个数据框中并删除重复的行:
Expected Output (Table 3):
1 7 9
7 3 2
1 4 7
12 -4 -1
我也希望输出在excel中。 下面的代码有什么问题?:
import pandas as pd
import numpy as np
df1 = pd.read_excel('Table 1.xlsx')
df2 = pd.read_excel('Table 2.xlsx')
frames = [df1,df2]
result = pd.concat(frames)
no_duplicates = result.drop_duplicates(False)
import xlsxwriter
workbook = xlsxwriter.Workbook('Table 3.xlsx', {'constant_memory': True})
worksheet = workbook.add_worksheet()
row = 0
for row, group in enumerate (no_duplicates):
for col in range(3):
worksheet.write (row, col, group[col])
workbook.close()
答案 0 :(得分:0)
尝试以下代码
import pandas as pd
from pandas import ExcelWriter
df1=pd.DataFrame([[1,7,9],[2,4,11],[7,3,2]])
df2=pd.DataFrame([[1,4,7],[12,-4,-1],[2,4,11]])
df=pd.concat([df1,df2]).drop_duplicates().reset_index(drop=True)
wr = ExcelWriter('./sample.xlsx')
df.to_excel(wr,'Sheet5',index=False)
wr.save()