Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 439
Jab Avatar asked Feb 18 '17 14:02

Jab


People also ask

How do you use Tolist in Python?

tolist(), used to convert the data elements of an array into a list. This function returns the array as an a. ndim- levels deep nested list of Python scalars. In simple words, this function returns a copy of the array elements as a Python list.

How do you convert a DataFrame to a list?

At times, you may need to convert your pandas dataframe to List. To accomplish this task, ' tolist() ' function can be used.

How do you convert a data frame to a series?

Convert DataFrame Column to Series In pandas, each column is represented as a Series hence it is very easy to convert the values of a DataFrame column to a Series. Use df. iloc[:,0] to get the selected column as a Series. This example converts the first column of the Pandas DataFrame to a series.

How do I make a list from a column in Python?

tolist() you can convert pandas DataFrame Column to List. df['Courses'] returns the DataFrame column as a Series and then use values. tolist() to convert the column values to list.


1 Answers

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
like image 72
Chuck Avatar answered Oct 10 '22 08:10

Chuck