要學(xué)數(shù)據(jù)挖掘與分析第一步當(dāng)然是要導(dǎo)入數(shù)據(jù)到程序當(dāng)中或者從程序中導(dǎo)出數(shù)據(jù)到本地文件當(dāng)中探橱,這里我使用pandas庫提供的函數(shù)來舉例導(dǎo)入和導(dǎo)出數(shù)據(jù)。本文所用的環(huán)境:python :3.5 pandas:0.19.2 numpy:1.12.1熔吗,sqlalchemy 1.1.9 如果你的環(huán)境和這樣不一樣可能會有 細(xì)微差別每币。
pandas支持的數(shù)據(jù)格式
pandas作為一個強(qiáng)大的數(shù)據(jù)處理包先壕,支持比較多的數(shù)據(jù)處理格式翔脱,下面是一些常見格式數(shù)據(jù)的讀取方法,更多請參考:鏈接
函數(shù) | 描述 |
---|---|
read_table(filepath_or_buffer[, sep, ...]) | 讀取普通分隔的數(shù)據(jù) |
read_csv(filepath_or_buffer[, sep, ...]) | 讀取csv格式的數(shù)據(jù) |
read_excel(io[, sheetname, header, ...]) | 讀取excel格式的數(shù)據(jù) |
read_json([path_or_buf, orient, typ, dtype, ...]) | 讀取json格式的數(shù)據(jù) |
read_html(io[, match, flavor, header, ...]) | 讀取html格式的 數(shù)據(jù) |
read_sql(sql, con[, index_col, ...]) | 讀取數(shù)據(jù)庫中的數(shù)據(jù) |
前面兩個一般用的比較多媒鼓。
常見格式讀取示例
read_table舉例
example.csv是一個用逗號隔開的數(shù)據(jù)格式届吁。所以可以用read_table讀取,需要指定間隔符為逗號绿鸣。
import pandas as pd
data_csv = pd.read_table('example.csv',sep=',')
print("data_csv:")
print(data_csv)
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
但是有時隔符是不定個數(shù)的空格疚沐,這時可以用正則表達(dá)式。
import pandas as pd
data_txt = pd.read_table('example.txt',sep='\s+')
print("data_txt:")
print(data_txt)
此處由于數(shù)據(jù)中列名比數(shù)據(jù)列少1潮模,read_table會推斷第一行為列名亮蛔。
A B C
aaa -0.264438 -1.026059 -0.619500
bbb 0.927272 0.302904 -0.032399
ccc -0.264273 -0.386314 -0.217601
ddd -0.871858 -0.348382 1.100491
read_csv舉例
import pandas as pd
data_csv2 = pd.read_csv('example.csv')
print("data_csv2:")
print(data_csv2)
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
read_exel示例
import pandas as pd
data_xlsx = pd.read_excel('example.xlsx')
print("data_xlsx:")
print(data_xlsx)
默認(rèn)是讀取第一個 sheet表格的,如果要制定讀取sheet表格則需要指定 sheetname參數(shù)
data_xlsx2 = pd.read_excel('example.xlsx',sheetname="Sheet2")
print("data_xlsx2:")
print(data_xlsx2)
a b c d message
0 11 12 13 4 hello
1 15 16 17 18 world
2 19 20 21 12 foo
更多參數(shù)可以參考官方手冊
read_json示例
data_json = pd.read_json('example.json')
print(data_json)
直接打開json文件的內(nèi)容如下:
[{"a": 1, "b": 2, "c": 3},
{"a": 4, "b": 5, "c": 6},
{"a": 7, "b": 8, "c": 9}]
讀取后 輸出的格式如下
a b c
0 1 2 3
1 4 5 6
2 7 8 9
read_sql擎厢、read_sql_table和read_sql_query示例
import pymysql
import pandas as pd
con = pymysql.connect(host="127.0.0.1",user="root",password="password",db="world")
data_sql=pd.read_sql("select * from city limit 10",con)
print(data_sql)
數(shù)據(jù)庫用的是mysql究流,數(shù)據(jù)是里面自帶的測試數(shù)據(jù)辣吃。
ID Name CountryCode District Population
0 1 Kabul AFG Kabol 1780000
1 2 Qandahar AFG Qandahar 237500
2 3 Herat AFG Herat 186800
3 4 Mazar-e-Sharif AFG Balkh 127800
4 5 Amsterdam NLD Noord-Holland 731200
在使用read_sql_table和read_sql_query時需要使用sqlalchemy對數(shù)據(jù)庫進(jìn)行連接。這里仍然使用mysql為例芬探,其他數(shù)據(jù)庫的鏈接方式有細(xì)微差別神得。
import pandas as pd
import pymysql
from sqlalchemy import create_engine
con = create_engine('mysql+pymysql://root:password@localhost:3306/world')
data_sql2 = pd.read_sql_table("city", con)
print(data_sql2)
data_sql3 = pd.read_sql_query("select * from city limit 5", con)
print(data_sql3)
本文示例代碼和文件下載地址:鏈接
提取密碼:4koh