Here is my answer to my own question:
import pandas as pd
# Sample data for df_diff
data_diff = {
'Key': ['Key2', 'Key3', 'Key4'],
'Column_1': ['Value2', 'Value3', 'Value5'],
'Column_2': ['Value3', 'Value4', 'Value6']
}
df_diff = pd.DataFrame(data_diff)
# Sample data for df_all
data_all = {
'Key': ['Key2', 'Key3', 'Key6'],
'Column_all_A': ['Value8', 'Value3', 'Value0'],
'Column_all_B': ['Value2', 'Value10', 'Value11']
}
df_all = pd.DataFrame(data_all)
# Function to find matching cells and apply red background to df_all
def highlight_matching_cells(row_all):
# Get the key value from the current row in df_all
key_value = row_all['Key']
# Filter the corresponding row in df_diff using the key value
row_diff = df_diff[df_diff['Key'] == key_value]
# Check if a matching row is found in df_diff
if not row_diff.empty:
# Iterate over columns in df_all (except the 'Key' column)
for col_all in row_all.index[1:]:
# Iterate over columns in the matching row of df_diff (except the 'Key' column)
for col_diff in row_diff.columns[1:]:
# Check if the cell value in df_all matches any cell value in the matching row of df_diff
if row_all[col_all] == row_diff[col_diff].iloc[0]:
# If a match is found, return a list with red background for the matched cell in df_all
return ['background-color: red' if col == col_all else '' for col in row_all.index]
# If no match is found, return a list with no background color for all cells in df_all
return ['' for _ in row_all.index]
# Apply the function to each row in df_all
df_highlighted = df_all.style.apply(highlight_matching_cells, axis=1)
# Display the highlighted DataFrame
df_highlighted
This give me the desired output:

However, does anyone have a more elegant and shorter way?
I would like to define a styler() function with formatting condition in it and the apply the format using df.style.apply() or df.style.applymap() to each of the matching cells.