Python 散景:AttributeError:'DataFrame' 对象没有属性 'tolist'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42316088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bokeh: AttributeError: 'DataFrame' object has no attribute 'tolist'
提问by Jab
I am new to pandas and bokeh and I am trying to create a scatter plot from a pandas dataframe. However, I keep getting the following error:
我是熊猫和散景的新手,我正在尝试从熊猫数据框创建散点图。但是,我不断收到以下错误:
new_data[colname] = df[colname].tolist()
AttributeError: 'DataFrame' object has no attribute 'tolist'
Using the dummy data from bokeh (from bokeh.sampledata.iris import flowers as data) the scatter works fine.
使用散景中的虚拟数据(来自 bokeh.sampledata.iris 导入鲜花作为数据)分散效果很好。
type tsneX tsneY +50.000 columns
0 A 53.828863 20.740931
1 B 57.816909 18.478468
2 A 55.913429 22.948167
3 C 56.603005 15.738954
scatter = Scatter(df, x='tsneX', y='tsneY',
color='type', marker='type',
title='t-sne',
legend=True)
Edit: I'm not using the tolist(), but the Scatter() of Bokeh does and produces the error below.
编辑:我没有使用 tolist(),但 Bokeh 的 Scatter() 使用并产生以下错误。
采纳答案by Jab
I solved the problem by first extracting the relevant columns from the dataframe.
我首先从数据框中提取相关列来解决这个问题。
df = df.loc[:, ('type', 'tsneX', 'tsneY')
scatter = Scatter(df, x='tsneX', y='tsneY',
color='type', marker='type',
title='t-sne',
legend=True)
回答by Chuck
You are using tolist
incorrectly. You want: .values
followed by tolist()
您使用tolist
不当。你想要:.values
其次tolist()
type tsneX tsneY
0 A 53.828863 20.740931
1 B 57.816909 18.478468
2 A 55.913429 22.948167
3 C 56.603005 15.738954
For the above dataframe, to get your X and Y values as a list you can do:
对于上述数据框,要将 X 和 Y 值作为列表,您可以执行以下操作:
tsneY_data = df['tsneY'].values.tolist()
>> [20.740931, 18.478468, 22.948167, 15.7389541]
tsneX_data = df['tsneX'].values.tolist()
>> [53.828863, 57.816909, 55.913429, 56.603005]
As you have tried to set this to the column of a new dataframe, you can do:
当您尝试将其设置为新数据框的列时,您可以执行以下操作:
new_data = pd.DataFrame()
new_data['tsneY'] = df['tsneY'].values.tolist()
> new_data
tsneY
0 20.740931
1 18.478468
2 22.948167
3 15.738954