pandas是基于numpy包擴(kuò)展而來(lái)的,因而numpy的絕大多數(shù)方法在pandas中都能適用。
pandas中我們要熟悉兩個(gè)數(shù)據(jù)結(jié)構(gòu)Series 和DataFrame
Series是類似于數(shù)組的對(duì)象,它有一組數(shù)據(jù)和與之相關(guān)的標(biāo)簽組成。
1 2 3 4 5 | import pandas as pd object=pd.Series([2,5,8,9]) print(object) |
結(jié)果為:
0 2
1 5
2 8
3 9
dtype: int64
結(jié)果中包含一列數(shù)據(jù)和一列標(biāo)簽
我們可以用values和index分別進(jìn)行引用
1 2 | print(object.values) print(object.index) |
結(jié)果為:
[2 5 8 9]
RangeIndex(start=0, stop=4, step=1)
我們還可以按照自己的意愿構(gòu)建標(biāo)簽
1 2 3 | object=pd.Series([2,5,8,9],index=['a','b','c','d']) print(object) |
結(jié)果為:
a 2
b 5
c 8
d 9
dtype: int64
我們還可以對(duì)序列進(jìn)行運(yùn)算
1 | print(object[object>5]) |
結(jié)果為
c 8
d 9
dtype: int64
也可以把Series看成一個(gè)字典,使用in進(jìn)行判斷
1 | print('a' in object) |
結(jié)果為:
True
另外,值是不能直接被索引到的
1 | print(2 in object) |
結(jié)果為:
False
Series中的一些方法,
isnull或者notnull可以用于判斷數(shù)據(jù)中缺失值情況
name或者index.name可以對(duì)數(shù)據(jù)進(jìn)行重命名
DataFrame數(shù)據(jù)框,也是一種數(shù)據(jù)結(jié)構(gòu),和R中的數(shù)據(jù)框類似
1 2 3 4 5 6 | data={'year':[2000,2001,2002,2003], 'income':[3000,3500,4500,6000]} data=pd.DataFrame(data) print(data) |
結(jié)果為:
income year
0 3000 2000
1 3500 2001
2 4500 2002
3 6000 2003
1 2 3 | data1=pd.DataFrame(data,columns=['year','income','outcome'], index=['a','b','c','d']) print(data1) |
結(jié)果為:
year income outcome
a 2000 3000 NaN
b 2001 3500 NaN
c 2002 4500 NaN
d 2003 6000 NaN
新增加列outcome在data中沒(méi)有,則用na值代替
索引的幾種方式
1 2 | print(data1['year']) print(data1.year) |
兩種索引是等價(jià)的,都是對(duì)列進(jìn)行索引,結(jié)果為:
a 2000
b 2001
c 2002
d 2003
Name: year, dtype: int64
對(duì)行進(jìn)行索引,則是另外一種形式
1 | print(data1.ix['a']) |
結(jié)果為:
year 2000
income 3000
outcome NaN
Name: a, dtype: object
1 | print(data1[1:3]) |
或者也可以用切片的形式
結(jié)果為:
year income outcome
b 2001 3500 NaN
c 2002 4500 NaN
增加和刪除列
1 | data1['money']=np.arange(4) |
增加列為money
year income outcome money
a 2000 3000 NaN 0
b 2001 3500 NaN 1
c 2002 4500 NaN 2
d 2003 6000 NaN 3
1 | del data1['outcome'] |
刪除列結(jié)果為:
year income money
a 2000 3000 0
b 2001 3500 1
c 2002 4500 2
d 2003 6000 3
pandas中的主要索引對(duì)象以及相對(duì)應(yīng)的索引方法和屬性
此外還有一個(gè)reindex函數(shù)可以重新構(gòu)建索引
1 2 3 4 5 6 7 8 | data={'year':[2000,2001,2002,2003], 'income':[3000,3500,4500,6000]} data1=pd.DataFrame(data,columns=['year','income','outcome'], index=['a','b','c','d']) data2=data1.reindex(['a','b','c','d','e']) print(data2) |
結(jié)果為:
1 2 | data2=data1.reindex(['a','b','c','d','e'],method='ffill') print(data2) |
使用方法后的結(jié)果為:
索引刪除以及過(guò)濾等相關(guān)方法
1 | print(data1.drop(['a'])) |
結(jié)果為:
1 | print(data1[data1['year']>2001]) |
結(jié)果為:
1 | print(data1.ix[['a','b'],['year','income']]) |
結(jié)果為 :
1 | print(data1.ix[data1.year>2000,:2]) |
結(jié)果為:
詳細(xì)的索引過(guò)濾方法如下:
dataframe的算法運(yùn)算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | data={'year':[2000,2001,2002,2003], 'income':[3000,3500,4500,6000]} data1=pd.DataFrame(data,columns=['year','income','outcome'], index=['a','b','c','d']) data2=pd.DataFrame(data,columns=['year','income','outcome'], index=['a','b','c','d']) data1['outcome']=range(1,5) data2=data2.reindex(['a','b','c','d','e']) print(data1.add(data2,fill_value=0)) |
結(jié)果為:
對(duì)dataframe進(jìn)行排序
1 2 3 4 | data=pd.DataFrame(np.arange(10).reshape((2,5)),index=['c','a'], columns=['one','four','two','three','five']) print(data) |
結(jié)果為:
1 | print(data.sort_index()) |
結(jié)果為:
1 | print(data.sort_index(axis=1)) |
結(jié)果為:
1 | print(data.sort_values(by='one')) |
結(jié)果為:
1 | print(data.sort_values(by='one',ascending=False)) |
結(jié)果為:
這里是對(duì)結(jié)果進(jìn)行降序排列
匯總以及統(tǒng)計(jì)描述
1 2 3 4 | data=pd.DataFrame(np.arange(10).reshape((2,5)),index=['c','a'], columns=['one','four','two','three','five']) print(data.describe()) |
結(jié)果為:
1 | print(data.sum()) |
結(jié)果為:
1 | print(data.sum(axis=1)) |
結(jié)果為:
詳細(xì)約簡(jiǎn)方法
相關(guān)描述統(tǒng)計(jì)函數(shù)
相關(guān)系數(shù)與協(xié)方差
1 2 3 4 | data=pd.DataFrame(np.random.random(20).reshape((4,5)),index=['c','a','b','c'], columns=['one','four','two','three','five']) print(data) |
結(jié)果為:
1 | print(data.one.corr(data.three)) |
one和three的相關(guān)系數(shù)為:
0.706077105725
1 | print(data.one.cov(data.three)) |
one和three的協(xié)方差為:
0.0677896135613
1 | print(data.corrwith(data.one)) |
one和所有列的相關(guān)系數(shù):
唯一值,成員資格等方法
1 2 3 | data=pd.Series(['a','a','b','b','b','c','d','d']) print(data.unique()) |
結(jié)果為:
[‘a(chǎn)’ ‘b’ ‘c’ ‘d’]
1 | print(data.isin(['b'])) |
結(jié)果為:
0 False
1 False
2 True
3 True
4 True
5 False
6 False
7 False
dtype: bool
1 | print(pd.value_counts(data.values,sort=False)) |
結(jié)果為:
d 2
c 1
b 3
a 2
dtype: int64
缺失值處理
1 2 3 | data=pd.Series(['a','a','b',np.nan,'b','c',np.nan,'d']) print(data.isnull()) |
結(jié)果為:
0 False
1 False
2 False
3 True
4 False
5 False
6 True
7 False
dtype: bool
1 | print(data.dropna()) |
結(jié)果為:
0 a
1 a
2 b
4 b
5 c
7 d
dtype: object
1 | print(data.ffill()) |
結(jié)果為:
0 a
1 a
2 b
3 b
4 b
5 c
6 c
7 d
dtype: object
1 | print(data.fillna(0)) |
結(jié)果為:
0 a
1 a
2 b
3 0
4 b
5 c
6 0
7 d
dtype: object
層次化索引
可以對(duì)數(shù)據(jù)進(jìn)行多維度的索引
1 2 3 4 | data = pd.Series(np.random.randn(10), index=[['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd'], [1, 2, 3, 1, 2, 3, 1, 2, 2, 3]]) print(data) |
結(jié)果為:
1 | print(data.index) |
結(jié)果為:
MultiIndex(levels=[[‘a(chǎn)’, ‘b’, ‘c’, ‘d’], [1, 2, 3]],
1 2 3 | labels=[[0, 0, 0, 1, 1, 1, 2, 2, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 1, 2]]) print(data['c']) |
結(jié)果為:
1 | print(data[:,2]) |
結(jié)果為:
1 | print(data.unstack()) |
結(jié)果為:
把數(shù)據(jù)轉(zhuǎn)換成為一個(gè)dataframe
1 | print(data.unstack().stack()) |
unstack()的逆運(yùn)算
了解這些,應(yīng)該可以進(jìn)行一些常規(guī)的數(shù)據(jù)處理了。
聯(lián)系客服