I am trying to color cells based on the value of the specific cell. I have a dataframe that includes data for the same product for two different dates. I want to highlight the cell for the latest date green or red depending on whether it has increased or decreased.
I am unable to figure out how to do this for each column in a way that functions as it should. It doesn't help that I am new to pandas style and am essentially learning by doing.
Any help is greatly appreciated
import pandas as pd
import numpy as np
# Create a sample dataframe (replace it with your actual data)
data = {
'ID': ['B-1', 'B-1', 'F-1', 'F-1'],
'Analysis Date': ['31/05/2023', '21/05/2023', '31/05/2023', '21/05/2023'],
'Col 1': [10.91, 10.89, 7.24, 7.27],
'Col 2': [1, 2, 0.76, 0.80]
}
df = pd.DataFrame(data)
# Set the 'Security' and 'Analysis Date' columns as the index
df.set_index(['ID', 'Analysis Date'], inplace=True)
# Define the custom function to highlight increased values using lambda
highlight_increased_risk = lambda x: ['background-color: red' if x[i] > x[i-1] else '' for i in range(len(x))]
# Apply the style function using applymap to each cell
styled_df = df.style.apply(highlight_increased_risk, axis=0)
# Display the styled dataframe
styled_df
I am unable to find an efficient method to identify cells which need to be highlighted and to apply styling to those specific ones. Especially since I am trying to isolate comparisons based on ID and date.
