Big data Analysis based on python-pandas data Reading (Code practice)
Our common data storage formats are csv, excel, txt, database and so on.
Data reading
There are some functions that can be used to read data in pandas. For example, read_csv, read_excel, read_table, read_sql, etc., what do these mean? You can understand it by looking at the suffix.
Let's learn about them through the sink code.
Txt file
Format: read_table (file path and file name, names= [column name 1, column name 2], sep= "",.)
Where names is the column name and defaults to the first line in the file as the column name
Sep is a delimiter and is empty by default
From pandas import read_table#txtdf=read_table (ringing Dpart pythonism workspaceanacondarz.txt') # View the first five rows of data df.head (5) # View the last two rows of data # df.tail (2)
The content of rz.txt is as follows
Csv file
Format: read_csv (file path and file name, names= [column name 1, column name 2], sep= "",.)
Explain the same as above, not nonsense
# csvfrom pandas import read_csvdf=read_csv (ringing Dpart pythonism workspaceanacondarz.csv') df
The content of rz.csv is as follows
Excel file
Format: read_excel (file path and file name, name of sheetname=sheet, header=0)
Sheetname can specify that the number of sheet,sheet reads starts at 0. If sheetname= [0J2] means to read the first and third sheet
A header of 0 indicates that the first line of the file is displayed as the header; if 1, the first line of the file is discarded and not displayed as the header.
# exelfrom pandas import read_exceldf=read_excel (ringing Dvu pythonism workspaceanacondarz.xlswords, sheetname='Sheet3') df
Mysql
Install pymysql first, and then install it through the pip command
Format: read_sql (sql statement to query, database link object)
Import pandas as pdimport pymysql# specific database link information to replace conn=pymysql.connect (host='xxxx',database='xxx',user='root',password='',port=3306,charset='utf8') sql='select * from a'r=pd.read_sql (sql,conn) # close database link conn.close () print (r.head (5))