reading data from the database pandas

Because there is a need to use pandos needs to do a data analysis, so here to do some recording this week.

Python with Pandas data analysis, the most common is Dataframe data structures,
here we introduce how to read the data Pandas Dataframe.

  1. Pandas read Mysql Mysql data to be read in the data, first install Mysqldb package. Suppose I database installed locally, fame MyUserName user, password mypassword, mydb to read the data in the database, then the corresponding code is as follows:
import pandas as pd
import MySQLdb
mysql_cn= MySQLdb.connect(host='localhost', port=3306,user='myusername', passwd='mypassword', db='mydb')
df = pd.read_sql('select * from test;', con=mysql_cn)    
mysql_cn.close()

The above code reading the test data to the table all the df, df is the data structure Dataframe.

  1. Pandas Pandas read data read data csv file csv file is much simpler, without additional installation package, suppose we want to read the data in test.csv, the corresponding code is as follows:
df = pd.read_csv(loggerfile, header=None, sep=',')

header = None indicates no head, sep = ',' field represents a delimiter between a comma.


Reflections on the data analysis of the
demand here is to obtain comparative information chain trend of last week's data, according to this week's data. However, since the data has been incremental updates to the database, you need to control your search using the time field with a need
to add a few here on sql database query
query today's data
select * from tj where to_days(now()) = to_days(tjsj)
query z yesterday's data
select * from tj where to_days(now()) - to_days(tjsj) = 1
query this week, data
select * from tj where tjsj >= subdate(curdate(),date_format(curdate(),'%w')-1) and tjsj <= subdate(curdate(),date_format(curdate(),'%w')-7)
query data last week
select * from tj where tjsj >= subdate(curdate(),date_format(curdate(),'%w')+6) and tjsj <= subdate(curdate(),date_format(curdate(),'%w')-0)

Demand complete stages are as follows:
1, using a database query sql, check out qualified last week's data. Query time last week, or last week's data is updated.
2, database query sql, check out qualified data this week. Query time this week, or this week's data is updated.
3, data analysis work, to get the data you want. 1,2 analytical data obtained, and calculates.
This work plan two days to complete.

Guess you like

Origin www.cnblogs.com/wangcc7/p/12556292.html