python自學篇十六[pandas——數據分析 (一):pandas安裝+Series+DataFrame]

概括:Numpy+Scipy+pandas+matplotlib

在這裏插入圖片描述

pandas——數據分析

基於numPy 的一種工具,爲了解決數據分析任務而創建的.Pandas 納入了大量庫和一些標準的數據模型,提供了高效地操作大型數據集所需的工具。

一.pandas安裝

Python的Anaconda發行版,已經安裝好pandas庫,不需要另外安裝

1.使用Anaconda界面安裝

選擇對應的pandas進行勾選安裝即可

2.使用Anaconda命令安裝

conda install pandas

3. 使用PyPi安裝命令安裝

pip install pandas

二. pandas引入約定

 from pandas import Series, DataFrame
 import pandas as pd

三. pandas基本數據結構

pandas中主要有兩種數據結構,分別是:

1.Series

一種類似於一維數組的對象,是由一組數據(各種NumPy數據類型)以及一組與之相關的數據標籤(即索引)組成。僅由一組數據也可產生簡單的Series對象
注意:Series中的索引值是可以重複的

1.通過一維數組創建

jupyter notebook運行代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd
arr=np.array([1,3,5,np.NaN,10])
series01=Series(arr)
series01
series01.dtype
series01.index
series01.values

結果:

0     1.0   #通過數組創建Series的時候,如果沒有指定索引值,則自動創建0-N的整數索引
1     3.0   #當Series對象創建好後可以通過index修改索引值
2     5.0
3     NaN
4    10.0
dtype: float64
dtype('float64')
RangeIndex(start=0, stop=5, step=1)
array([ 1.,  3.,  5., nan, 10.])

在這裏插入圖片描述
代碼:

series02=Series([10,20,30])
series02

series03.index=[u'語文',u'數學',u'英語']
series03

series04=Series(data=[10,20,30],dtype=np.float64,index=[u'語文',u'數學',u'英語'])
series04

結果:

0    10
1    20
2    30
dtype: int64

語文    10.0
數學    20.0
英語    30.0
dtype: float64

語文    10.0
數學    20.0
英語    30.0
dtype: float64

2.通過字典的方式創建

代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd
dict1={'王大錘':100,'牛子':60,'李鐵鋼':90}
#字典中的key成爲Series中的索引值,value成爲Series中的values
series05=Series(dict1)
print(series05)
print('index:',series05.index)
print('values',series05.values)
print('dtype',series05.dtype)

結果:

王大錘    100
牛子      60
李鐵鋼     90
dtype: int64
index: Index(['王大錘', '牛子', '李鐵鋼'], dtype='object')
values [100  60  90]
dtype int64

3.Series值的獲取方式

1.方括號+索引

讀取對應索引的數據,有可能返回多條數據
代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd
series06=Series(data=[10,20,30],dtype=np.float64,index=[u'語文',u'數學',u'英語'])
print(series06)
print()
print(u"series['語文']",series06[u'語文'])#方括號+索引獲取值  

結果:

語文    10.0
數學    20.0
英語    30.0
dtype: float64

series['語文'] 10.0
2.方括號+下標值
  • 讀取對應下標值的數據,下標值的取值範圍爲:[0,len(Series.values))
  • 另外下標值也可以是負數,表示從右往左獲取數據
    代碼:
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
series06=Series(data=[10,20,30],dtype=np.float64,index=[u'語文',u'數學',u'英語'])
print(series06)
print()
print("series[0]=",series06[0])#方括號+下標值獲取值
print("series[-1]=",series06[-1])

結果:

語文    10.0
數學    20.0
英語    30.0
dtype: float64

series[0]= 10.0
series[-1]= 30.0
3.Series獲取多個值

類似NumPy中的ndarray的切片操作,通過方括號+下標值/索引值+冒號(:)的形式來截取series對象中的一部分數據。
Series的運算
代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd
series06=Series(data=[10,20,30],dtype=np.float64,index=[u'語文',u'數學',u'英語'])
print(series06)
print()
print(series06[1:])#切片獲取多個值
print()
print(series06[u'語文':u'數學'])     

結果:

語文    10.0
數學    20.0
英語    30.0
dtype: float64

數學    20.0
英語    30.0
dtype: float64

語文    10.0
數學    20.0
dtype: float64

4.Series的運算

  • NumPy中的數組運算,在Series中都保留了,均可以使用,並且Series進行數組運算的時候,索引與值之間的映射關係不會發生改變
  • 注意:其實在操作Series的時候,基本上可以把Series看成NumPy中的ndarray數組來進行操作。ndarray數組的絕大多數操作都可以應用到Series上
    在這裏插入圖片描述

在這裏插入圖片描述

5.Series缺失值檢測

pandas中的isnull和notnull兩個函數可以用於在Series中檢測缺失值,這兩個函數的返回時一個布爾類型的Series

代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd

scores=Series({"Tom":100,"Bob":90,"Peter":80})
print(scores)
print()

print(pd.isnull(scores))
print()

print(pd.notnull(scores))
print()

print(scores[pd.isnull(scores)])#過濾出爲缺失值的項
print()

print(scores[pd.notnull(scores)])#過濾出不是缺失值的項
print()

結果:

Tom      100
Bob       90
Peter     80
dtype: int64

Tom      False
Bob      False
Peter    False
dtype: bool

Tom      True
Bob      True
Peter    True
dtype: bool

Series([], dtype: int64)

Tom      100
Bob       90
Peter     80
dtype: int64

代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd

scores=Series({"Tom":100,"Bob":90,"Peter":80})
print(scores)
print()
new_index=["Tom","Bob","Jone","Peter"]
scores=Series(scores,index=new_index)
print(scores)

結果:(NaN表示一個缺失值或者NA值)

Tom      100
Bob       90
Peter     80
dtype: int64

Tom      100.0
Bob       90.0
Jone       NaN
Peter     80.0
dtype: float64

6.Series自動對齊

當多個series對象之間進行運算的時候,如果不同series之間具有不同的索引值,那麼運算會自動對齊不同索引值的數據,如果某個series沒有某個索引值,那麼最終結果會賦值爲NaN

代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd

s1=Series([12,23,45],index=['p1','p2','p3'])
s2=Series([23,42,13],index=['p3','p4','p6'])
print("=====s1+s2=====")
print(s1+s2)
print()
print("=====s1*s2=====")
print(s1*s2)
print()
print("=====s1/s2=====")
print(s1/s2)

結果:

=====s1+s2=====
p1     NaN
p2     NaN
p3    68.0
p4     NaN
p6     NaN
dtype: float64

=====s1*s2=====
p1       NaN
p2       NaN
p3    1035.0
p4       NaN
p6       NaN
dtype: float64

=====s1/s2=====
p1         NaN
p2         NaN
p3    1.956522
p4         NaN
p6         NaN
dtype: float64

7.Series及其索引的name屬性

Series對象本身以及索引都具有一個name屬性,默認爲空,根據需要可以進行賦值操作
代碼:

import numpy as np
from pandas import Series, DataFrame
import pandas as pd

scores=Series({"Tom":100,"Bob":90,"Peter":80})
scores.name=u'數學' #取名字,Name: 數學
scores.index.name=u'期末考試成績'#標題
print(scores)

結果:

期末考試成績
Tom      100
Bob       90
Peter     80
Name: 數學, dtype: int64

2.DataFrame

1.通過二維數組創建

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

2.通過字典的方式創建

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

3.索引對象

  • 不管是Series還是DataFrame對象,都有索引對象。
  • 索引對象負責管理軸標籤和其它元數據(eg:軸名稱等等)
  • 通過索引可以從Series、DataFrame中獲取值或者對某個索引值進行重新賦值
  • Series或者DataFrame的自動對齊功能是通過索引實現的

4.DataFrame數據獲取

可以直接通過列索引獲取指定列的數據, eg: df[column_name]
如果需要獲取指定行的數據的話,需要通過ix方法來獲取對應行索引的行數據,eg: df.ix[index_name]
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章