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

Jab picture Jab · Feb 18, 2017 · Viewed 44.3k times · Source

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.

Answer

Chuck picture Chuck · Feb 18, 2017

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