Reputation: 777
I am currently working on a simple demo that reads an excel file with two columns and extracts the data from each row of the columns into two different lists.
That all works fine, but when I try to plot the data using matplotlib I get this error.
TypeError: float() argument must be a string or a number
I understand that the error is being caused by the float values in the second list, the should just be integers. What I do not understand is how to prevent them from becoming floats when they are read into the list.
[text:u'Joe', text:u'Sam', text:u'Bobby', text:u'Tom', text:u'Jane']
[number:23.0, number:36.0, number:19.0, number:31.0, number:28.0]
I have tried typecasting in several different places, but always get the same error.
TypeError: int() argument must be a string or a number, not 'Cell'
Here is the method that I am using for this program
def bargraph(self, fileName, xlabel, title):
people = list()
dollars = list()
workbook = xlrd.open_workbook(fileName)
worksheet = workbook.sheet_by_index(0)
for row in range(worksheet.nrows):
people.append(worksheet.cell(row ,0))
for row in range(worksheet.nrows):
dollars.append(worksheet.cell(row, 1))
plt.bar(people, dollars, 1, color="blue")
plt.xlabel(xlabel)
plt.title(title)
plt.show()
Upvotes: 1
Views: 1689
Reputation: 1602
This error occurs because with worksheet.cell(row, idx) you retrieve a Cell instance. So you're creating a list of Cells, while the plotting library requires a list of ints.
Try looking at using the functions worksheet.cell_value and worksheet.cell_type of the xlrd library. For example try:
for row in range(worksheet.nrows):
people.append(worksheet.cell_value(row ,0))
for row in range(worksheet.nrows):
dollars.append(worksheet.cell_value(row, 1))
Upvotes: 1