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

爱⌒轻易说出口 提交于 2019-12-07 04:14:08

问题


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.

   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.


回答1:


You are using tolist incorrectly. You want: .values followed by 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:

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



回答2:


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)


来源:https://stackoverflow.com/questions/42316088/bokeh-attributeerror-dataframe-object-has-no-attribute-tolist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!