pandas是基於numpy包擴展而來的,於是numpy的絕大多數方法在pandas中都能適用。python
pandas中咱們要熟悉兩個數據結構Series 和DataFrame算法
Series是相似於數組的對象,它有一組數據和與之相關的標籤組成。數組
import pandas as pd object=pd.Series([2,5,8,9]) print(object)
結果爲:數據結構
0 2
1 5
2 8
3 9
dtype: int64dom
結果中包含一列數據和一列標籤
咱們能夠用values和index分別進行引用函數
print(object.values) print(object.index)
結果爲:3d
[2 5 8 9]
RangeIndex(start=0, stop=4, step=1)對象
咱們還能夠按照本身的意願構建標籤blog
object=pd.Series([2,5,8,9],index=['a','b','c','d']) print(object)
結果爲:排序
a 2
b 5
c 8
d 9
dtype: int64
咱們還能夠對序列進行運算
print(object[object>5])
結果爲
c 8
d 9
dtype: int64
也能夠把Series當作一個字典,使用in進行判斷
print('a' in object)
結果爲:
True
另外,值是不能直接被索引到的
print(2 in object)
結果爲:
False
Series中的一些方法,
isnull或者notnull能夠用於判斷數據中缺失值狀況
name或者index.name能夠對數據進行重命名
DataFrame數據框,也是一種數據結構,和R中的數據框相似
data={'year':[2000,2001,2002,2003], 'income':[3000,3500,4500,6000]} data=pd.DataFrame(data) print(data)
結果爲:
income year
0 3000 2000
1 3500 2001
2 4500 2002
3 6000 2003
data1=pd.DataFrame(data,columns=['year','income','outcome'], index=['a','b','c','d']) print(data1)
結果爲:
year income outcome
a 2000 3000 NaN
b 2001 3500 NaN
c 2002 4500 NaN
d 2003 6000 NaN
新增長列outcome在data中沒有,則用na值代替
索引的幾種方式
print(data1['year']) print(data1.year)
兩種索引是等價的,都是對列進行索引,結果爲:
a 2000
b 2001
c 2002
d 2003
Name: year, dtype: int64
對行進行索引,則是另一種形式
print(data1.ix['a'])
結果爲:
year 2000
income 3000
outcome NaN
Name: a, dtype: object
print(data1[1:3])
或者也能夠用切片的形式
結果爲:
year income outcome
b 2001 3500 NaN
c 2002 4500 NaN
增長和刪除列
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
del data1['outcome']
刪除列結果爲:
year income money
a 2000 3000 0
b 2001 3500 1
c 2002 4500 2
d 2003 6000 3
pandas中的主要索引對象以及相對應的索引方法和屬性
此外還有一個reindex函數能夠從新構建索引
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)
結果爲:
data2=data1.reindex(['a','b','c','d','e'],method='ffill') print(data2)
使用方法後的結果爲:
索引刪除以及過濾等相關方法
print(data1.drop(['a']))
結果爲:
print(data1[data1['year']>2001])
結果爲:
print(data1.ix[['a','b'],['year','income']])
結果爲 :
print(data1.ix[data1.year>2000,:2])
結果爲:
詳細的索引過濾方法以下:
dataframe的算法運算
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))
結果爲:
對dataframe進行排序
data=pd.DataFrame(np.arange(10).reshape((2,5)),index=['c','a'], columns=['one','four','two','three','five']) print(data)
結果爲:
print(data.sort_index())
結果爲:
print(data.sort_index(axis=1))
結果爲:
print(data.sort_values(by='one'))
結果爲:
print(data.sort_values(by='one',ascending=False))
結果爲:
這裏是對結果進行降序排列
彙總以及統計描述
data=pd.DataFrame(np.arange(10).reshape((2,5)),index=['c','a'], columns=['one','four','two','three','five']) print(data.describe())
結果爲:
print(data.sum())
結果爲:
print(data.sum(axis=1))
結果爲:
詳細約簡方法
相關描述統計函數
相關係數與協方差
data=pd.DataFrame(np.random.random(20).reshape((4,5)),index=['c','a','b','c'], columns=['one','four','two','three','five']) print(data)
結果爲:
print(data.one.corr(data.three))
one和three的相關係數爲:
0.706077105725
print(data.one.cov(data.three))
one和three的協方差爲:
0.0677896135613
print(data.corrwith(data.one))
one和全部列的相關係數:
惟一值,成員資格等方法
data=pd.Series(['a','a','b','b','b','c','d','d']) print(data.unique())
結果爲:
['a' 'b' 'c' 'd']
print(data.isin(['b']))
結果爲:
0 False
1 False
2 True
3 True
4 True
5 False
6 False
7 False
dtype: bool
print(pd.value_counts(data.values,sort=False))
結果爲:
d 2
c 1
b 3
a 2
dtype: int64
缺失值處理
data=pd.Series(['a','a','b',np.nan,'b','c',np.nan,'d']) print(data.isnull())
結果爲:
0 False
1 False
2 False
3 True
4 False
5 False
6 True
7 False
dtype: bool
print(data.dropna())
結果爲:
0 a
1 a
2 b
4 b
5 c
7 d
dtype: object
print(data.ffill())
結果爲:
0 a
1 a
2 b
3 b
4 b
5 c
6 c
7 d
dtype: object
print(data.fillna(0))
結果爲:
0 a
1 a
2 b
3 0
4 b
5 c
6 0
7 d
dtype: object
層次化索引
能夠對數據進行多維度的索引
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)
結果爲:
print(data.index)
結果爲:
MultiIndex(levels=[['a', 'b', 'c', 'd'], [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'])
結果爲:
print(data[:,2])
結果爲:
print(data.unstack())
結果爲:
把數據轉換成爲一個dataframe
print(data.unstack().stack())
unstack()的逆運算
瞭解這些,應該能夠進行一些常規的數據處理了。