0

I'm trying to create a a treemap in Plotly Express. If I manually enter the column names, the treemap is created. Passing a list of the same column names throws a ValueError.

Essentially I have a dataframe with column names 3,4,5,6,7,8,9,10

Because these column names will vary depending on the data, I'd like to pass these as a list to Plotly express.

Manually entering them as per below, will work

treemap_fig = px.treemap(df_split_folders, path=[px.Constant("Chart Title"), 3,4,5,6,7,8,9,10],values='count', color_discrete_sequence=px.colors.qualitative.Antique)

but passing them as a list does not. E.g

# create list of column names from dataframe
total_cols = df_split_folders.columns.values.tolist()

#pass as a list
treemap_fig = px.treemap(df_split_folders, path=[px.Constant("Chart Title"), 3,4,5,6,7,8,9,10],values='count', color_discrete_sequence=px.colors.qualitative.Antique)

I've also tried converting the column name list to a string, but no dice.

Edit: Here is a Minimum Reproducible Example:

import pandas as pd
import plotly.express as px

# dictionary with list object in values
details = {
    'url' : ['/category', '/category', '/product', '/product'],
    'folder' : ['/games', '/playstation', '/cars', '/cycling'],
    'type' : ['adventure', 'action', 'action', 'adventure'],
}

df = pd.DataFrame(details, index = ['a', 'b', 'c', 'd'])
df['count'] = 1

treemap_fig = px.treemap(df, path=[px.Constant("Discovered / Crawled Not Indexed"), 'url', 'folder', 'type'], values='count', color_discrete_sequence=px.colors.qualitative.Antique)
treemap_fig.show()

this code works as expected and produces a treemap, however - if I try and pass the column names as a variable, it no longer references the dataframe column names. e.g.

cols = 'url', 'folder', 'type'

treemap_fig = px.treemap(df, path=[px.Constant("Discovered / Crawled Not Indexed"), **cols**], values='count', color_discrete_sequence=px.colors.qualitative.Antique)
    treemap_fig.show()
Lee Roy
  • 297
  • 1
  • 11
  • Would it be better if the column names in the data frame are in string format and the px.treemap specification is also in string format? `px.treemap(df_split_folders, path=[px.Constant("Chart Title"), '3','4','5','6','7','8','9','10']` – r-beginners Feb 06 '22 at 12:59
  • It doesn't make a difference unfortunately. If I pass a list of the column names (even as strings) it just assigns them a value of 1, rather than referencing the dataframe column names. e.g. I added a prefix of 'folder_' to each of the dataframe column names. If I explicitly add the column names in, it works. I was hoping I could create a list of the column names and just pass it the variable, but it doesn't reference the dataframe columns that way. – Lee Roy Feb 06 '22 at 13:20
  • I commented on the example in the [official reference](https://plotly.com/python/treemaps/#treemap-of-a-rectangular-dataframe-with-discrete-color-argument-in-pxtreemap), but the content of the column is a string, right? – r-beginners Feb 06 '22 at 13:26
  • Correct, the columns contents are strings. I use values="count" which gives me the totals). – Lee Roy Feb 06 '22 at 13:39
  • 1
    I don't know if I can give you an answer, but if you provide some sample data, you may get an answer. – r-beginners Feb 06 '22 at 13:42
  • Good shout! I've added in a minimum reproducible example. – Lee Roy Feb 06 '22 at 13:59
  • 1
    I can handle unpacking with `*col`. – r-beginners Feb 06 '22 at 14:07
  • Ah! So obvious now you say it! Thank you! – Lee Roy Feb 06 '22 at 14:10
  • if you post as an answer @r-beginners I'll accept it as the best answer. Thanks again! – Lee Roy Feb 06 '22 at 14:15

1 Answers1

1

To expand a column variable, you can use an asterisk to expand it. Please refer to the official reference for details.

cols = 'url', 'folder', 'type'

treemap_fig = px.treemap(df, 
                         path=[px.Constant("Discovered / Crawled Not Indexed"), *cols],
                         values='count',
                         color_discrete_sequence=px.colors.qualitative.Antique)

treemap_fig.show()
r-beginners
  • 31,170
  • 3
  • 14
  • 32