Df和Df1
df是模拟代表
Country Category Brand
South Africa Health Proctor
America Shoe Nike
America Clothing Forever21
UK Car Rover
df1
Country Category Brand
South Africa Health Proctor
America Shoe Nike
America Clothing Forever21
UK £!"4 11111
我想比较两者并创建并返回一个表格匹配的新表格。
解决方案1:Df3匹配检查布尔输出
Country Category Brand
True True True
True True True
True True True
True False False
所需解决方案2:使用openpyxl输出Df3
我最好使用openpyxl比较两个数据帧表并返回条件格式excel文件。
我正在使用的套件(如果有帮助):
python 2.6 pandas 0.16 numpy 1.9.2 openpyxl 2.4.8
答案 0 :(得分:0)
我尝试第二种解决方案
red_color = 'ffc7ce'
diff_val = np.where((df!= df1).any(1) == True)
df_result = pd.DataFrame(diff_val.columns)
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(template, engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df_result.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Applying a conditional format to the cell range.
for item in diff_result:
df_result.append(df.iloc[item], df1.iloc[item])
worksheet.conditional_format(df1, {'type': 'unique', 'format':red_color})
# # Close the Pandas Excel writer and output the Excel file.
writer.save()