1

I used to read the data from CSV file, while I just imported all CSV data in SQL database, but I have difficulty in extracting data using Python from SQL.

My original code of read CSV is like this:

import pandas as pd
stock_data = pd.read_csv(filepath_or_buffer='stock_data_w.csv', parse_dates=[u'date'], encoding='gbk')
stock_data[u'change_weekly'] = stock_data.groupby(u'code')[u'change'].shift(-1)

Now I want to read data from SQL, here is my code, but it doesn't work and I am not sure how to sort it out:

import pandas as pd
import MySQLdb

db = MySQLdb.connect(host='localhost', user='root', passwd='232323', db='test', port=3306)
cur = db.cursor()
cur.execute("SELECT * FROM stock_data_w")

stock_data = pd.DataFrame(data=cur.fetchall(),  columns=[i[0] for i in cur.description])
stock_data[u'change_weekly'] = stock_data.groupby(u'code')[u'change'].shift(-1)

the error is: "raise PandasError('DataFrame constructor not properly called!') pandas.core.common.PandasError: DataFrame constructor not properly called!"

2
  • DataFrame is available now. Go to IDE and use print stock_data.head() to see the data and stock_data.columns to understand what you have in data Commented Dec 7, 2015 at 4:56
  • Thanks very much! Finally I sorted out under you help. Commented Dec 7, 2015 at 21:30

1 Answer 1

3

Use below way to convert your cursor object to crate data frame.

stock_data = pd.DataFrame(data=cursor.fetchall(), index=None,
                         columns=cursor.keys())
print stock_data

In mysqldb, columns=[i[0] for i in cursor.description]

or

Make your connection with alchemy and use,

stock_data = pd.read_sql("SELECT * from stock_data_w",
                                      con= cnx,parse_dates=['date'])

I'm not sure whether mysql.connector is supported in pandas read_sql(). You can give a try and let us know :)

Sign up to request clarification or add additional context in comments.

6 Comments

Hi WoodChopper, thanks very much for your reply! I tried both, the first replied an AttributeError: 'MySQLCursor' object has no attribute 'keys'. The second doesnt work either. I also installed MySQLdb, how can I group data under MySQLdb please? Thanks
I'm sure mysqldb cursor will have keys.
Hi use columns=[i[0] for i in cursor.description] in mysqldb
Thanks, Thanks, I changed the code with importing MySQLdb. Columns is ok now, but parse_dates has prob, : (... I have updated the post, you can see my code and error above.
Hi remove that parse_dates=['date'] it will infer that as date column
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.