比较两个数据帧并创建一个布尔值 table 或标志
compare two dataframes and create a boolean table or flag
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
我想比较两者并创建 return 一个新的 table 显示它们匹配,即
解决方案 1:Df3 匹配检查布尔输出
Country Category Brand
True True True
True True True
True True True
True False False
所需的解决方案 2:使用 openpyxl 的 Df3 输出
Desired openxyl output
我最好使用 openpyxl 来比较两个数据帧 tables 和 return 条件格式 excel 文件。
我正在使用的套件(如果有帮助):
python2.6
pandas 0.16
numpy 1.9.2
openpyxl 2.4.8
我尝试第二种解决方案
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()
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
我想比较两者并创建 return 一个新的 table 显示它们匹配,即
解决方案 1:Df3 匹配检查布尔输出
Country Category Brand
True True True
True True True
True True True
True False False
所需的解决方案 2:使用 openpyxl 的 Df3 输出
Desired openxyl output
我最好使用 openpyxl 来比较两个数据帧 tables 和 return 条件格式 excel 文件。
我正在使用的套件(如果有帮助):
python2.6 pandas 0.16 numpy 1.9.2 openpyxl 2.4.8
我尝试第二种解决方案
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()