Pandas 比较两个数据帧并创建布尔表或标志

Pandas 比较两个数据帧并创建布尔表或标志,pandas,numpy,openpyxl,python-2.6,Pandas,Numpy,Openpyxl,Python 2.6,Df和Df1 df是模拟代表 Country Category Brand South Africa Health Proctor America Shoe Nike America Clothing Forever21 UK Car Rover df1 我想比较两者,创建并返回一个显示它们匹配的新表,即 解决方案1:Df3匹配检查布尔输出 Country Category Brand True True

Df和Df1

df是模拟代表

Country      Category Brand
South Africa Health   Proctor
America      Shoe     Nike
America      Clothing Forever21  
UK           Car      Rover
df1

我想比较两者,创建并返回一个显示它们匹配的新表,即

解决方案1:Df3匹配检查布尔输出

Country Category Brand
True    True     True 
True    True     True 
True    True     True 
True    False    False
所需解决方案2:openpyxl的Df3输出

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()

我最好使用openpyxl比较两个数据帧表并返回条件格式excel文件

我正在使用的工具包(如果有帮助):

python 2.6 熊猫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_res=df==df1
不起作用。你试过df_res=df.as_matrix==df1.as_matrix了吗。这些是可以进行元素比较的numpy数组。到目前为止,您做了哪些尝试?您确实需要离开Python 2。6@charlie克拉克,我不能离开2.6。由于限制。我尝试过使用,但没有达到我的目标。