I'm a complete Python/MySQL newbie - trying to retrieve and manipulate data from a MySQL DB over a VPN/LAN from an Ubuntu/Python shell.
I've imported all the modules I think I need - MySQLdb, decimal, scipy, scipy.stats, numpy. I have been able to open the connector to the MySQL DB and retrieve the data of interest. My problem is as follows
1. I open a connection with
mydb = MySQLdb.connect(host = "xxx", port = yyy, user = "zzz", passwd = "nnnn", db = "whatineed")
cursor = mydb.cursor()
2. For some reason, each entry is a tuple though I am retrieving a single column from the MySQL table with a command
cursor.execute("SELECT difference FROM growth_record")
3. Data from the DB comes back in the format
((Decimal('-3.0600'),), (Decimal('-0.8500'),), (Decimal('-1.1900'),), (Decimal('-1.7000'),), (Decimal('0.4800'),),...
4. I am able to pull out the individual values by doing
result = cursor.fetchall()
for i in range(len(result)):
a = result[i][0]
OR to get a floating point result
for i in range(len(result)):
a = float(str(result[i][0]))
5. My problems
a) I am not able to create an array with the assignment
a[i] = result[i][0] - keep getting a type mismatch. I'd like to create an array with the direct results without the Tuple returned by the DB.
b) I am not able to perform statistical analyses from scipy.stats (e.g. mean) with the results. The functions keep expecting floating point arguments, the numbers returned from the DB are decimal numbers.
Is there an obviously simple way to do either?
Thank you
Bapi
Hi
Good work. Here's an interactive session, which should help. I've defined result as the tuple in your point 3, and I've imported decimal and scipy.stats.
Then - In [16]: a=[]
-
-
In [17]: for r in result:
-
....: a.append(float(r[0]))
-
....:
-
....:
-
-
In [18]: a
-
Out[18]:
-
[-3.0600000000000001,
-
-0.84999999999999998,
-
-1.1899999999999999,
-
-1.7,
-
0.47999999999999998]
-
-
In [19]: mean(a)
-
Out[19]: -1.264
-
For numpy arrays, instead of lists, the append function generally doesn't work/exist in my experience. So it's better to define it up front at the right size, and then update the elements: - In [21]: from numpy import *
-
-
In [22]: a=zeros((5))
-
-
In [23]: a
-
Out[23]: array([ 0., 0., 0., 0., 0.])
-
-
In [26]: for i,r in enumerate(result):
-
....: a[i]=r[0]
-
....:
-
....:
-
-
In [27]: a
-
Out[27]: array([-3.06, -0.85, -1.19, -1.7 , 0.48])
-
-
In [28]: mean(a)
-
Out[28]: -1.264
-
In general the assignment (a[i]=foo) only works if a[i] already exists, which may be the source of problem a.
Also, things like mean only work on lists or arrays or similar iterables.
Good luck, and let us know how it goes!
2 2639 Glenton 391
Recognized Expert Contributor
Hi
Good work. Here's an interactive session, which should help. I've defined result as the tuple in your point 3, and I've imported decimal and scipy.stats.
Then - In [16]: a=[]
-
-
In [17]: for r in result:
-
....: a.append(float(r[0]))
-
....:
-
....:
-
-
In [18]: a
-
Out[18]:
-
[-3.0600000000000001,
-
-0.84999999999999998,
-
-1.1899999999999999,
-
-1.7,
-
0.47999999999999998]
-
-
In [19]: mean(a)
-
Out[19]: -1.264
-
For numpy arrays, instead of lists, the append function generally doesn't work/exist in my experience. So it's better to define it up front at the right size, and then update the elements: - In [21]: from numpy import *
-
-
In [22]: a=zeros((5))
-
-
In [23]: a
-
Out[23]: array([ 0., 0., 0., 0., 0.])
-
-
In [26]: for i,r in enumerate(result):
-
....: a[i]=r[0]
-
....:
-
....:
-
-
In [27]: a
-
Out[27]: array([-3.06, -0.85, -1.19, -1.7 , 0.48])
-
-
In [28]: mean(a)
-
Out[28]: -1.264
-
In general the assignment (a[i]=foo) only works if a[i] already exists, which may be the source of problem a.
Also, things like mean only work on lists or arrays or similar iterables.
Good luck, and let us know how it goes!
Glenton,
Thanks. Both methods worked. I appreciate your response.
Regards
Bapi
p.s. do you happen to know of any math modules that work with the Decimal data type?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: chris |
last post by:
I open MySQL Manager and connect to my database.
I then insert one record in to an InnoDB table in MySQL (4.0 and 4.1
alpha hosted on XP running mysqld --console).
I then retrieve the record...
|
by: Stormblade |
last post by:
Hey all,
I have an existing JSP web application which retrieved data from a
SQLServer database and displayed it. The data contained Unicode chars. This
worked fine. I changed databases/JDBC...
|
by: John Bowman |
last post by:
Hi,
A couple Q's , so I've come to the experts<g>.
1) I need a definitive answer to the following debate. I've got a couple
developers who disagree on the following question. We've got an XML...
|
by: invinfo |
last post by:
keywords: mysql accounting currency decimal fixed "floating point"
Quoting the manual:
DECIMAL)]
If D is omitted, the default is 0. If M is omitted, the default is 10.
All basic calculations...
|
by: Thomas Nelson |
last post by:
Sorry if this is a FAQ, but I couldn't find a good summary through
google. What kinds of statistical analysis tools exist in python? I
really just need t-tests, chi-squared test, and other such...
|
by: Wim Cossement |
last post by:
Hello,
I was wondering if there are a few good pages and/or examples on how to
process form data correctly for putting it in a MySQL DB.
Since I'm not used to using PHP a lot, I already found...
|
by: John Henry |
last post by:
I am looking for a simple Python function for handling a set of time
series data. For instance, I might have the following raw data
(year's worth):
1/1/2005 12:00 AM 11.24
1/1/2005 12:10...
|
by: Talbot Katz |
last post by:
Greetings Pythoners!
I hope you'll indulge an ignorant outsider. I work at a financial software
firm, and the tool I currently use for my research is R, a software
environment for statistical...
|
by: yucefrizk |
last post by:
hello all,
I'm trying to write a code to retreive data from a database to an excel file, I achieved my code and everything is going good by running it through unix, but when I create a webpage to...
|
by: Laphan |
last post by:
Hi All
My ASP driven site has always used MySQL as the DB backbone, mainly as the
hosting costs of MySQL are far cheaper than SQL Server at the mo, and it has
worked fine until my ISP thought...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |