I have two dataframes of identical size, df_a and df_b. df_a only contains numbers and is styled using a colormap (either using xlsxwriter or pandas styling). df_b contains mixed values. Is it possible to copy the resulting style from one dataframe to the other such that the background color of df_b[i,j] equals the background color of df_a[i,j]?
In the example below the first element of df_b should be red, the second yellow, and so on.
Formatting code for df_a (based on the xlsxwriter example)
# Create a Pandas dataframe from some data.
df_a = pd.DataFrame({'col_a': [10, 20, 30, 20, 15, 30, 45]})
df_b = pd.DataFrame({'col_b': ['a', 'a', 'c', 'd', 'e', 'f', 'g']})
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas_conditional.xlsx', engine='xlsxwriter')
# Convert the dataframe to an XlsxWriter Excel object.
df_a.to_excel(writer, sheet_name='Sheet1')
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']
# Apply a conditional format to the cell range.
worksheet.conditional_format('B2:C8', {'type': '3_color_scale'})
# Close the Pandas Excel writer and output the Excel file.
writer.save()

