1. 程式人生 > >Python資料分析與挖掘學習筆記(2)使用pandas進行資料匯入

Python資料分析與挖掘學習筆記(2)使用pandas進行資料匯入

一、匯入pandas模組:  

import pandas as pda

二、匯入CSV格式資料:

#資料匯入
i=pda.read_csv("E:/hexun.csv")

    可對匯入的資料進行統計以及按列排序:

#統計
i.describe()
#排序
i.sort_values(by="21") #21是列名

三、匯入Excel格式資料:

#excel
j=pda.read_excel("E:/abc.xls")

四、匯入資料框ql資料(以mysql以及sqlite為例):

#mysql
import pymysql
conn=pymysql.connect(host="127.0.0.1",user="root",db="hexun")
sql="select * from myhexun"
k=pda.read_sql(sql,conn)

#sqlite
import sqlite3
conn = sqlite3.connect('E:/dangdang.db')
sql="select * from house"
k=pda.read_sql(sql,conn)

五、匯入html中的表格資料:

以某網頁為例:

提取該網頁的表格資料程式碼:

#html中的表格資訊
l=pda.read_html("http://www.nseac.com/html/234/680687.html")

六、匯入txt文字資料(注意檔案要使用UTF-8編碼):

#匯入文字
n=pda.read_table("E:/a.txt")