question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API to style categorical variables

See original GitHub issue

It is often useful to style columns having a pd.CategoricalDtype, by either:

  • being supplied a mapping from category name/code to background colour.
  • being supplied a mapping from category name/code to Styler.
  • selecting len(dtype.categories) colours from a matplotlib colormap (qualitative or otherwise).

So for example we might allow for:

StyleFrame(df).add_categorical_conditional_formatting(style_map={"a": Styler(font_color="red"), "b": Styler(font_color="green"), "c": Styler(font_color="blue")})

or

StyleFrame(df).add_categorical_conditional_formatting(style_bg_cmap="Pastel1"})

which would perform the following for each column

cmap = matplotlib.cm.get_cmap(style_bg_cmap)
col_style_map = dict(zip(col.dtype.categories, cmap(len(col.dtype.categories))))

Conditional formatting would be applied using a CellIsRule("=", category) for each category value in each column in range.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DeepSpace2commented, Jun 15, 2021

Ah, I see.

The current API does support conditional formatting, but only using ColorScaleRule (wrapped by StyleFrame.add_color_scale_conditional_formatting).

However, the above example can be implemented without it, using quite an explicit code:

style_map = {"a": Styler(font_color="red"), "b": Styler(font_color="green"), "c": Styler(font_color="blue")}
sf = StyleFrame({'1': ['a', 'b'],
                 '2': ['c', 'd']})
for value, styler in style_map.items():
    for col in sf.columns:
        sf.apply_style_by_indexes(sf[sf[col] == value], styler, cols_to_style=col)

generating

image

0reactions
stale[bot]commented, Sep 19, 2021

This issue has been automatically marked as stale because it has not had activity in the last 60 days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fitting models using R-style formulas - Statsmodels
Since version 0.5.0, statsmodels allows users to fit statistical models using R-style formulas. Internally, statsmodels uses the patsy package ...
Read more >
Categorical Data — xgboost 1.7.2 documentation
The basic idea is create dataframe with category feature type, and tell XGBoost to use it by setting the enable_categorical parameter. See Getting...
Read more >
3 Ways to Encode Categorical Variables for Deep Learning
How to integer encode and one hot encode categorical variables for modeling. ... How to Use the Keras Functional API for Deep Learning....
Read more >
Unique types | Documentation - ArcGIS Developers
Different APIs have different levels of support for data-driven visualization. Unique types, Class breaks, Visual variables, Time, Multivariate, Predominance ...
Read more >
Visualizing categorical data — seaborn 0.12.1 documentation
If one of the main variables is “categorical” (divided into discrete groups) it may be helpful to use a more specialized approach to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found