pandas
對於數據分析的人員來說都是必須熟悉的第三方庫,pandas
在科學計算上有很大的優勢,特別是對於數據分析人員來說,相當的重要。python中有了Numpy
,但是Numpy
還是比較數學化,還需要有一種庫能夠更加具體的代表數據模型,我們都非常的清楚在數據處理中EXCEL
扮演着非常重要的作用,表格的模式是數據模型最好的一種展現形式。
pandas
是對表格數據模型在python上的模擬,它有簡單的像SQL
對數據的處理,能夠方便的在python上實現。
pandas 的安裝
pandas
在python上的安裝同樣的使用pip
進行:
pip install pandas
pandas 創建對象
pandas
有兩種數據結構:Series
和 DataFrame
。
Series
Series
像python中的數據list
一樣,每個數據都有自己的索引。從list
創建 Series
。
>>> import pandas as pd
>>> s1 = pd.Series([100,23,'bugingcode'])
>>> s1
0 100
1 23
2 bugingcode
dtype: object
>>>
在Series
中添加相應的索引:
>>> import numpy as np
>>> ts = pd.Series(np.random.randn(365), index=np.arange(1,366))
>>> ts
在index中設置索引值是一個從1到366的值。
Series
的數據結構最像的是python中的字典,從字典中創建Series
:
sd = {'xiaoming':14,'tom':15,'john':13}
s4 = pd.Series(sd)
這時候可以看到Series
已經是自帶索引index。
pandas
本身跟 python的另外一個第三方庫Matplotlib
有很多的連接,Matplotlib
一個最經常用到的是用來展示數據的,如果還對Matplotlib
不了解的話,后面的章節會進行介紹,現在先拿過來直接用下,如果還沒有安裝的話,一樣的用pip
命令安裝 pip install Matplotlib
, 展示如下數據:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(365), index=np.arange(1,366))
ts.plot()
plt.show()
一個不規則的圖形,在數據分析中,時間是一個重要的特性,因為很多數據都是跟時間是有關系的,銷售額跟時間有關系,天氣跟時間有關系。。。,在pandas
中也提供了關於時間的一些函數,使用date_range
生成一系列時間。
>>> pd.date_range('01/01/2017',periods=365)
DatetimeIndex(['2017-01-01', '2017-01-02', '2017-01-03', '2017-01-04',
'2017-01-05', '2017-01-06', '2017-01-07', '2017-01-08',
'2017-01-09', '2017-01-10',
...
'2017-12-22', '2017-12-23', '2017-12-24', '2017-12-25',
'2017-12-26', '2017-12-27', '2017-12-28', '2017-12-29',
'2017-12-30', '2017-12-31'],
dtype='datetime64[ns]', length=365, freq='D')
>>>
之前我們的圖形不規則,有一個原因是數據不是連續的,使用cumsum
讓數據連續:
如下:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(365), index=pd.date_range('01/01/2017',periods=365))
ts = ts.cumsum()
ts.plot()
plt.show()
DataFrame
DataFrame
相當於Series
一維的一個擴展,是一種二維的數據模型,相當於EXcel表格中的數據,有橫豎兩種坐標,橫軸很Series
一樣使用index,豎軸用columns 來確定,在建立DataFrame
對象的時候,需要確定三個元素:數據,橫軸,豎軸。
df = pd.DataFrame(np.random.randn(8,6), index=pd.date_range('01/01/2018',periods=8),columns=list('ABCDEF'))
print df
數據如下:
A B C D E F
2018-01-01 0.712636 0.546680 -0.847866 -0.629005 2.152686 0.563907
2018-01-02 -1.292799 1.122098 0.743293 0.656412 0.989738 2.468200
2018-01-03 1.762894 0.783614 -0.301468 0.289608 -0.780844 0.873074
2018-01-04 -0.818066 1.629542 -0.595451 0.910141 0.160980 0.306660
2018-01-05 2.008658 0.456592 -0.839597 1.615013 0.718422 -0.564584
2018-01-06 0.480893 0.724015 -1.076434 -0.253731 0.337147 -0.028212
2018-01-07 -0.672501 0.739550 -1.316094 1.118234 -1.456680 -0.601890
2018-01-08 -1.028436 -1.036542 -0.459044 1.321962 -0.198338 -1.034822
在數據分析的過程中,很常見的一種情況是數據直接從excel
或者cvs
過來,可以excel
中讀取數據到DataFrame
,數據在 DataFrame
中進行處理:
df = pd.read_excel('data.xlsx',sheet_name= 'Sheet1')
print df
同樣的有保存數據到excel
中 to_excel
。
處理cvs數據的函數是:read_cvs
和 to_cvs
,處理HDF5的函數為 read_hdf
和 to_hdf
。
訪問DataFrame
可以跟二位數組一樣的訪問方式:
print df['A']
帶出橫軸標簽:
2018-01-01 0.712636
2018-01-02 -1.292799
2018-01-03 1.762894
2018-01-04 -0.818066
2018-01-05 2.008658
2018-01-06 0.480893
2018-01-07 -0.672501
2018-01-08 -1.028436
同樣的可以指定某一個元素:
print df['A']['2018-01-01']
對數組進行切片出來,認清橫軸和縱軸:
>>> import pandas as pd
>>> df = pd.read_excel('data.xlsx',sheet_name= 'Sheet1')
>>> df[:][0:3]
A B C D E F
2018-01-01 0.712636 0.546680 -0.847866 -0.629005 2.152686 0.563907
2018-01-02 -1.292799 1.122098 0.743293 0.656412 0.989738 2.468200
2018-01-03 1.762894 0.783614 -0.301468 0.289608 -0.780844 0.873074
>>>
DataFrame
涉及的較多的函數,接下來會有更多的介紹。
更多教程:大家來編程