gpt4 book ai didi

python - 为什么 bokeh 提示缺少 tolist 属性?

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:38 25 4
gpt4 key购买 nike

我正在尝试调整 stacked bar plot example一些现有数据。

我的数据框结构与示例中提供的结构类似。可以查到here作为 CSV。

这是我的代码:

from bokeh.charts import Bar, output_file, show
from bokeh.charts.attributes import cat, color
from bokeh.charts.operations import blend

bar = Bar(
df,
values=blend('farfetch','harrods', 'lyst', 'mrporter','nastygal','neimanmarcus','netaporter','theoutnet','yoox', name='retailers', labels_name='retailers'),
label=cat(columns='index', sort=False),
stack=cat(columns='retailers', sort=False),
tooltips=[('retailers', '@retailers'), ('brands', '@index')]
)

show(bar)

执行此代码后,我得到以下堆栈跟踪:

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-26-65462e21dce7> in <module>()
4 label=cat(columns=df['index'].tolist(), sort=False),
5 stack=cat(columns='retailers', sort=False),
----> 6 tooltips=[('retailers', '@retailers'), ('brands', '@index')]
7 )

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/bokeh/charts/builders/bar_builder.pyc in Bar(data, label, values, color, stack, group, agg, xscale, yscale, xgrid, ygrid, continuous_range, **kw)
318 kw['y_range'] = y_range
319
--> 320 chart = create_and_build(BarBuilder, data, **kw)
321
322 # hide x labels if there is a single value, implying stacking only

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/bokeh/charts/builder.pyc in create_and_build(builder_class, *data, **kws)
60 # create the new builder
61 builder_kws = {k: v for k, v in kws.items() if k in builder_props}
---> 62 builder = builder_class(*data, **builder_kws)
63
64 # create a chart to return, since there isn't one already

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/bokeh/charts/builder.pyc in __init__(self, *args, **kws)
280
281 # handle input attrs and ensure attrs have access to data
--> 282 attributes = self._setup_attrs(data, kws)
283
284 # remove inputs handled by dimensions and chart attributes

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/bokeh/charts/builder.pyc in _setup_attrs(self, data, kws)
308
309 """
--> 310 source = ColumnDataSource(data.df)
311 attr_names = self.default_attributes.keys()
312 custom_palette = kws.get('palette')

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/bokeh/models/sources.pyc in __init__(self, *args, **kw)
83 if not isinstance(raw_data, dict):
84 if pd and isinstance(raw_data, pd.DataFrame):
---> 85 raw_data = self._data_from_df(raw_data)
86 else:
87 raise ValueError("expected a dict or pandas.DataFrame, got %s" % raw_data)

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/bokeh/models/sources.pyc in _data_from_df(df)
105 new_data = {}
106 for colname in df:
--> 107 new_data[colname] = df[colname].tolist()
108 if index.name:
109 new_data[index.name] = index.tolist()

/Users/lthibault/Python/antidox/buisint/analysis/venv/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name)
2667 if name in self._info_axis:
2668 return self[name]
-> 2669 return object.__getattribute__(self, name)
2670
2671 def __setattr__(self, name, value):

AttributeError: 'DataFrame' object has no attribute 'tolist'

为什么会发生这种情况?

最佳答案

据我所知,这是当 name 时发生的错误。和labels_name values的参数是一样的。

就你而言,它们都是 retailers ,因此您可能希望将标签更改为其他内容,例如rtlrs 。您还需要相应地更改堆栈和工具提示中的引用。

关于python - 为什么 bokeh 提示缺少 tolist 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com