Plotly Express Treemap: How to use with value that is a percentage when aggregating up the path (but have underlying data)

I’m currently using Plotly Express (Open to using Plotly) and trying to build a treemap that renders % (Percentages) for the value but trying to understand how to handle this for the parents/path in the data.

Consider a dataframe as follows:

| parent_1  | parent_2  | metric_1_numerator   | metric_2_denominator   | p_metric  |
| --------- | --------- | -------------------- | ---------------------- | --------- |
| A         | A.1       | 10                   | 20                     | .5        |
| A         | A.2       | 5                    | 25                     | .2        |
| B         | B.1       | 10                   | 100                    | .1        |

To use a treemap I would have a path that would go parent_1 → parent_2. Ultimately the value I want to render is metric_1_numerator / metric_2_denominator. However, the edge case is that when I am at parent_1 the underlying data needs to be summed SUM(metric_1_numerator) / SUM(metric_2_denominator). Thus the value aggregated for Parent_1 would be:

A: SUM(10+5) / SUM(20+25) = .333
B: SUM(10) / SUM(100) = .1

The values argument for px.treemap (plotly.express.treemap — 5.10.0 documentation) says:

  • values (str or int or Series or array-like) – Either a name of a column in data_frame, or a pandas Series or array_like object. Values from this column or array_like are used to set values associated to sectors.

So I’m not sure how to make use of it here.

Any insight on how to handle this?

Thanks -

bumpity bump bump :frowning:

Hello @kee,

I don’t think that you can do this with a dataframe, you’d have to construct exactly how you’d want stuff to orient.