'DataFrame' object has no attribute 'tolist' when I try to convert an excel file to a list

Viewed 3741

I am trying to Read this excel file on my laptop which has only one column and I wish to transfer it to a list by pandas using:

years = pd.read_excel(r"/Users/vijayaswani/Downloads/years.xlsx").tolist()

but I get the error

'DataFrame' object has no attribute 'tolist'

This is weird for me because I had a csv file earlier which I used pretty much the same code to read and transfer to a list and it works fine.

What is wrong with this code and how can I get this excel file in a list?

(My ultimate goal is to get a list which I can transfer to a Tkinter Combobox)

1 Answers

You might have a data frame with one column. Try squeeze() to coerce it to a pandas Series:

years = (pd.read_excel(r"/Users/vijayaswani/Downloads/years.xlsx")
           .squeeze()
           .tolist())
Related