Pandas库学习笔记
生活随笔
收集整理的這篇文章主要介紹了
Pandas库学习笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import pandas as pd
兩個數據類型:Series,DataFrame
pandas是基于Numpy實現的擴展庫,提供了高效地操作大型數據集所需的工具。
Series類型由一組數據和與之相關的數據索引組成。
In [4]: d=pd.Series(range(5)) #自動索引 In [5]: d Out[5]: 0 0 1 1 2 2 3 3 4 4 dtype: int64In [6]: d=pd.Series(range(5),index=['a','b','c','d','e']) #自定義索引 In [7]: d Out[7]: a 0 b 1 c 2 d 3 e 4 dtype: int64直接傳入字典:
In [10]: d=pd.Series({'a':1,'b':2,'c':3}) In [11]: d Out[11]: a 1 b 2 c 3 dtype: int64或從ndarray類型創建:
In [16]: d=pd.Series(np.arange(5),index=np.arange(9,4,-1)) In [17]: d Out[17]: 9 0 8 1 7 2 6 3 5 4 dtype: int64.index獲取索引,.values獲得數據值
?
DataFrame類型由共用相同索引的一組列構成,是一個表格行的數據類型,既有行索引,也有列索引,常用與表達二維數據。
1.從一維ndarray對象字典創建:
In [40]: d=pd.DataFrame(np.arange(20).reshape(4,5))In [41]: d Out[41]: 0 1 2 3 4 0 0 1 2 3 4 1 5 6 7 8 9 2 10 11 12 13 14 3 15 16 17 18 19In [43]: dt={'one':pd.Series([1,2,3],index=['a','b','c']),...: 'two':pd.Series([9,8,7,6],index=['a','b','c','d'])}In [45]: d=pd.DataFrame(dt)In [46]: d Out[46]: one two a 1.0 9 b 2.0 8 c 3.0 7 d NaN 6In [47]: pd.DataFrame(dt,index=['b','c','d'],columns=['two','three']) Out[47]: two three b 8 NaN c 7 NaN d 6 NaN2.從列表類型的字典創建:
In [50]: dt={'one':[1,2,3,4],'two':[9,8,7,6]}In [51]: d=pd.DataFrame(dt,index=['a','b','c','d'])In [52]: d Out[52]: one two a 1 9 b 2 8 c 3 7 d 4 6?
重新索引 ?.reindex():
In [2]: d1={'name':['Alice','Bob','Tony'],...: 'gender':['f','m','m'],...: 'age':[18,20,25]}In [5]: d=pd.DataFrame(d1,index=['c1','c2','c3']) In [6]: d Out[6]: age gender name c1 18 f Alice c2 20 m Bob c3 25 m TonyIn [7]: d=d.reindex(['c3','c2','c1']) In [8]: d Out[8]: age gender name c3 25 m Tony c2 20 m Bob c1 18 f AliceIn [9]: d=d.reindex(columns=['name','gender','age']) In [10]: d Out[10]: name gender age c3 Tony m 25 c2 Bob m 20 c1 Alice f 18索引類型的常用方法:
In [11]: new1=d.columns.insert(3,'birthday')In [12]: new1 Out[12]: Index([u'name', u'gender', u'age', u'birthday'], dtype='object')In [17]: newd=d.reindex(columns=new1,fill_value='0101')In [18]: newd Out[18]: name gender age birthday c3 Tony m 25 0101 c2 Bob m 20 0101 c1 Alice f 18 0101?
In [29]: newd.drop('c1') #drop和delete的區別 Out[29]: name gender age birthday c3 Tony m 25 0101 c2 Bob m 20 0101In [32]: n=newd.index.delete(2) In [33]: newd=newd.reindex(index=n)In [34]: newd Out[34]: name gender age birthday c3 Tony m 25 0101 c2 Bob m 20 0101?.sort_index(axis=0,ascending=True) ?根據索引進行排序,默認升序。
?.sort_values()
基本的統計分析函數:
In [7]: b Out[7]: 0 1 2 3 4 c 0 1 2 3 4 b 5 6 7 8 9 a 10 11 12 13 14 d 15 16 17 18 19In [8]: b.describe() Out[8]: 0 1 2 3 4 count 4.000000 4.000000 4.000000 4.000000 4.000000 mean 7.500000 8.500000 9.500000 10.500000 11.500000 std 6.454972 6.454972 6.454972 6.454972 6.454972 min 0.000000 1.000000 2.000000 3.000000 4.000000 25% 3.750000 4.750000 5.750000 6.750000 7.750000 50% 7.500000 8.500000 9.500000 10.500000 11.500000 75% 11.250000 12.250000 13.250000 14.250000 15.250000 max 15.000000 16.000000 17.000000 18.000000 19.000000In [9]: type(b.describe()) Out[9]: pandas.core.frame.DataFrameIn [10]: b.describe().ix['max'] Out[10]: 0 15.0 1 16.0 2 17.0 3 18.0 4 19.0 Name: max, dtype: float64In [11]: b.describe()[2] Out[11]: count 4.000000 mean 9.500000 std 6.454972 min 2.000000 25% 5.750000 50% 9.500000 75% 13.250000 max 17.000000 Name: 2, dtype: float64?數據的相關性:
協方差:
對于兩個事物X,Y ,
如果他們的協方差>0,X和Y正相關;
協方差<0,X和Y負相關;
協方差=0,獨立無關。
? .cov()
?
Pearson相關系數:
.corr()
?
轉載于:https://www.cnblogs.com/lovealways/p/7098352.html
總結
以上是生活随笔為你收集整理的Pandas库学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 丑数 Ugly Number
- 下一篇: 《iOS应用软件设计之道》—— 2.11