Hello all,
I recently made a mysql database using python and imported an excel file into the database. However, I am having some issues with some of the python. Using the following code, I have selected 11 numbers from a table in the database.

# c.execute('SELECT REF_PNL FROM actual_data_table WHERE REF_ENTRY_TIME>"2010-06-29 09:45:00" and REF_ENTRY_TIME<"2010-06-29 10:00:00"')
# result1=c.fetchall()

The results come out as follows:

>>>result1
(('15.64',), ('-5.2',), ('24.25',), ('22.18',), ('14.52',), ('21.8',), ('21.2804',), ('-0.13',), ('15.48',), ('2.2',), ('-6.81',))

This is fine, however, I can't seem to do any computations with these numbers. I'm trying to take the average of these numbers using numpy. But then I get this error:

TypeError: cannot perform reduce with flexible type

The result looks like a tuple within a tuple or something. So is there anyway to break the results down into regular computable numbers?

The best way is to convert this into a list of floating point numbers ...

result1 = (('15.64',), ('-5.2',), ('24.25',), ('22.18',), ('14.52',), 
('21.8',), ('21.2804',), ('-0.13',), ('15.48',), ('2.2',), ('-6.81',))

data_list = [float(item[0]) for item in result1]

import pprint
pprint.pprint(data_list)

print('-'*30)

average = sum(data_list)/len(data_list)
print("average = %0.2f" % average)

""" result >>>
[15.640000000000001,
 -5.2000000000000002,
 24.25,
 22.18,
 14.52,
 21.800000000000001,
 21.2804,
 -0.13,
 15.48,
 2.2000000000000002,
 -6.8099999999999996]
------------------------------
average = 11.38
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.