繁体   English   中英

"将数据框列名称列表传递给 Plotly Express Treemap"

[英]Passing a list of dataframe column names to Plotly Express Treemap

我正在尝试在 Plotly Express 中创建树形图。 如果我手动输入列名,则会创建树形图。 传递相同列名的列表会引发 ValueError。

本质上,我有一个列名为 3、4、5、6、7、8、9、10 的数据框

因为这些列名会因数据而异,所以我想将它们作为列表传递给 Plotly express。

按照下面手动输入它们,将工作

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)

但将它们作为列表传递不会。 例如

# 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)

我也尝试将列名列表转换为字符串,但没有骰子。

编辑:这是一个最小的可重现示例:

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()

此代码按预期工作并生成树图,但是 - 如果我尝试将列名作为变量传递,它不再引用数据框列名。 例如

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()

要展开列变量,您可以使用星号来展开它。 详情<\/a>请参考官方参考。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM