The error you're encountering is due to the incorrect use of the gmap parameter in the background_gradient method. In pandas, background_gradient does not accept a gmap argument. Instead, it applies a gradient based on the values in the current DataFrame. Here's how to fix it:
✅ Corrected Code
Remove the gmap=rel_val parameter and adjust the styling as needed:
dfs = []
for save_tag, length_stats in all_results.items():
for ref in ['total_char_len', 'total_base_len']:
df = pd.DataFrame.from_dict(length_stats, orient='index').map(lambda d: d['total_tokens_len'] / d[ref])
df = df[[c for c in pretokenizers_to_vis if c in df.columns]]
df.loc['mean'] = df.mean()
rel_val = df.div(df.mean(axis=1), axis=0)
# Remove 'gmap=rel_val' and use the default gradient
styled_df = df.style.highlight_min(axis=1, props='font-weight:bold; text-decoration:underline;') \
.background_gradient(cmap='RdYlGn_r') \
.set_caption(f"<div style='font-size: 14pt; font-weight: bold; color: #333; padding: 10px; background-color: #ddd; border: 2px solid #339'>Compression ratios (#tokens/{ref}) for {save_tag}</div>") \
.format("{:.3f}")
dfs.append(HTML(styled_df.to_html()))
🧠 Why This Works
- The
background_gradient method in pandas uses the current DataFrame's values by default. If you want to base the gradient on rel_val, you need to apply the gradient to rel_val instead.
- To use
rel_val for the gradient, you can create a new styled DataFrame from rel_val:
styled_rel_val = rel_val.style.background_gradient(cmap='RdYlGn_r')
Then combine it with the original df styling if needed.
🛠️ Alternative: Use rel_val for Gradient
If you want the gradient to reflect the values in rel_val, do this:
# Apply gradient to rel_val (not df)
styled_rel_val = rel_val.style.background_gradient(cmap='RdYlGn_r')
# Combine with other styles
dfs.append(HTML(styled_rel_val.to_html()))
🔍 Key Notes
gmap is not a valid parameter for background_gradient. Remove it.
- If you need to use
rel_val for the gradient, create a new styled DataFrame from rel_val.
- Ensure all DataFrames (
df, rel_val) have consistent shapes and column names for styling to work correctly.
Let me know if you need further clarification!