http://www.cnblogs.com/pengsixiong/p/5050833.html
一:CSV數據
一】:導入數據
1)從CSV文件讀入數據:pd.read_csv("文件名"),默認以逗號為分隔符
D:\data\ex1.csv文件內容: D:\data\ex2.csv文件內容
a,b,c,d,message 1,2,3,4,hello
1,2,3,4,hello 5,6,7,8,world
5,6,7,8,world 9,10,11,12,foo
9,10,11,12,foo
In [3]: df1 = pd.read_csv('D:\data\ex1.csv') #打開后默認添加index為從0自增長,columns默認用第一行數據
In [4]: df1
Out[4]:
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
In [15]: df2 = pd.read_csv('D:\data\ex2.csv')
In [16]: df2
Out[16]:
1 2 3 4 hello
0 5 6 7 8 world
1 9 10 11 12 foo
In [17]: df2 = pd.read_csv('D:\data\ex2.csv',header=None) #header參數指定columns都為從0自增長的數
In [18]: df2
Out[18]:
0 1 2 3 4
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
In [8]: df2 = pd.read_csv('D:\data\ex2.csv',names=list('abcde')) #用names參數指定columns的值
In [9]: df2
Out[9]:
a b c d e
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
In [13]: df2 = pd.read_csv('D:\data\ex2.csv',names=list('abcde'),index_col='e') #用index_col用指定的columns首元素作為index
In [14]: df2
Out[14]:
a b c d
e
hello 1 2 3 4
world 5 6 7 8
foo 9 10 11 12
2)其他格式:pd.read_table('文件名', sep='划分依據'),划分依據可用正則表達式【\s:空格等不可見字符】
注:read_table方法幾乎可以讀所有的表格型數據,包括txt,csv等等
D:\data\ex3.txt D:\data\ex4.txt
A B C D A B C
aaa -0.2 -1.02 -0.62 aaa -0.2 -1.02 -0.62
bbb 0.93 0.3 -0.03 bbb 0.93 0.3 -0.03
ccc -0.26 -0.39 -0.22 ccc -0.26 -0.39 -0.22
ddd -0.87 -0.35 1.1 ddd -0.87 -0.35 1.1
In [37]: df1 = pd.read_table('D:\data\ex3.txt',sep='\s+')
In [38]: df1
Out[38]:
A B C #以最小列數為准,取dataframe數據,且第一行數據作為columns,剩下的如果第一列作為多出則作為index,否者從0自增數作為index
aaa -0.20 -1.02 -0.62
bbb 0.93 0.30 -0.03
ccc -0.26 -0.39 -0.22
ddd -0.87 -0.35 1.10
In [44]: df2 = pd.read_table('D:\data\ex4.txt',sep='\s+')
In [45]: df2
Out[45]:
D A B C
0 aaa -0.20 -1.02 -0.62
1 bbb 0.93 0.30 -0.03
2 ccc -0.26 -0.39 -0.22
3 ddd -0.87 -0.35 1.10
3)擴展技巧
read_csv/read_table函數參數


D:\data\ex5.csv D:\data\ex6.csv
#hey! something,a,b,c,d,message
a,b,c,d,message one,1,2,3,4,NA
#just wanted to make things two,5,6,,8,world
#who are you three,9,10,11,12,foo
1,2,3,4,hello
5,6,7,8,world
9,10,11,12,foo
In [46]: df5 = pd.read_csv('D:\data\ex5.csv')
In [47]: df5
Out[47]:
#hey!
a b c d message
#just wanted to make things NaN NaN NaN NaN
#who are you NaN NaN NaN NaN
1 2 3 4 hello
5 6 7 8 world
9 10 11 12 foo
In [48]: df5 = pd.read_csv('D:\data\ex5.csv',skiprows=[0,2,3])
In [49]: df5
Out[49]:
a b c d message
0 1 2 3 4 hello
1 5 6 7 8 world
2 9 10 11 12 foo
In [59]: df5 = pd.read_csv('D:\data\ex6.csv',nrows = 2)
In [60]: df5
Out[60]:
something a b c d message
0 one 1 2 3 4 NaN
1 two 5 6 NaN 8 world
In [55]: df6 = pd.read_csv('D:\data\ex6.csv',na_values={'message':['foo','NA'],'something':['two']})
In [56]: df6
Out[56]:
something a b c d message
0 one 1 2 3 4 NaN
1 NaN 5 6 NaN 8 world
2 three 9 10 11 12 NaN
二】 數據的寫出:to_csv('文件名' , [index=..., header=...] )
In [7]: df = pd.read_csv('D:\data\ex1.csv',header=None)
In [8]: df
Out[8]:
0 1 2 3 4
0 a b c d message
1 1 2 3 4 hello
2 5 6 7 8 world
3 9 10 11 12 foo
In[9]:df.to_csv('D:\data\out1.csv')
In[10]:df.to_csv('D:\data\out2.csv',index=False,header=False) #即是把index和columns都棄掉,header表示columns
二:JSON格式
Json類型基本數據類型有對象(字典),數組(列表),字符串,數值,bool 以及 null。
注:若是字典,鍵的類型必須是string
In [22]: js = """{
....: "name":"Wes",
....: "places_lived":["US","Spain","Germany"],
....: "pet":null,
....: "siblings":[{"name":"Scott","age":25,"pet":"Zuko"},
....: {"name":"Katie","age":33,"pet":"Cisco"}]}
....: """
In [26]: import json
In [27]: data = json.loads(js) #將json格式轉化為python格式
In [28]: data
Out[28]:
{u'name': u'Wes',
u'pet': None,
u'places_lived': [u'US', u'Spain', u'Germany'],
u'siblings': [{u'age': 25, u'name': u'Scott', u'pet': u'Zuko'},
{u'age': 33, u'name': u'Katie', u'pet': u'Cisco'}]}
In [29]: #a_js = json.dumps(data) #將python格式轉化為json格式
#最簡單構造方法就是提取其中的數據,注意columns list中的值對應json數據中的需要提取的鍵並將其作為columns
In [31]: siblings = pd.DataFrame(data['siblings'],columns=['name','age'])
In [32]: siblings
Out[32]:
name age
Scott 25
Katie 33
三:XML與HTML
pass
四:二進制 179
pass
五:Excel 180
pass
七:HTML 與 Web API 181
許多網站提供基於json格式的數據API,通過request等庫可以獲取
pass
六:數據庫 182
pass
七:MongDB
184
