1. 程式人生 > >Python資料分析-Pandas(Series與DataFrame)

Python資料分析-Pandas(Series與DataFrame)

Pandas介紹:

  pandas是一個強大的Python資料分析的工具包,是基於NumPy構建的。

Pandas的主要功能:
  1)具備對其功能的資料結構DataFrame、Series
  2)整合時間序列功能
  3)提供豐富的數學運算和操作
  4)靈活處理缺失資料

pyhton裡面安裝、引入方式:
  安裝方法:pip install pandas
  引用方法:import pandas as pd

 

pands的陣列的建立:

建立空的的值

import pandas as pd
s = pd.Series()
print(s)  #Series([], dtype: float64)

傳入一個列表

data=['a','b','c','d']
res=pd.Series(data)
print(res)

'''結果
0    a
1    b
2    c
3    d
這裡沒有傳遞任何索引,因此預設情況下,它分配了從0到len(data)-1的索引,即:0到3
'''

傳一個字典

data = {'a' : 0, 'b' : 1, 'c' : 2}
s = pd.Series(data)
print(s)
'''結果
a    0
b    1
c    2
dtype: int64

注意 - 字典鍵用於構建索引。

'''

 

從標量建立索引:

如果資料是標量值,則必須提供索引。將按照索引重複該值進行匹配

res=pd.Series(0, index=['a','b','c','d'])
print(res)

'''結果
a    0
b    0
c    0
d    0

'''

 

自指定索引值:

res=pd.Series(['a','b','c','d'],index=['a_index','b_index','c_index','d_index'])
print(res)

'''結果
a_index    a
b_index    b
c_index    c
d_index    d

'''

 

從具有位置的系列中訪問資料(取值):

重點理解:陣列是從零開始計數的,第一個位置儲存再零位置)

檢視index 、 values的值:

#檢視陣列的index值
print(res.index)

#檢視陣列的value值
print(res.values)


#取值(根據預設第零位開始取)
print(res[0])  #a

取前三個值(不包括定義的最後一個數值)

res=pd.Series(['a','b','c','d'],index=['a_index','b_index','c_index','d_index'])

#取前三個值(不包括3)
print(res[:3]) #是個物件可以 res[:3].values
'''結果

  a_index a
  b_index b
  c_index c
  dtype: object

'''

取後三個值:

print(res[-3:])

'''結果
b_index    b
c_index    c
d_index    d
dtype: object

'''

 

使用索引標籤檢索資料並設定資料:

修改value值

res=pd.Series(['a','b','c','d'],index=['a_index','b_index','c_index','d_index'])
print(res)
res['a_index']='new_a'
print(res)

'''結果

a_index    new_a
b_index        b
c_index        c
d_index        d

'''

copy複製資料並修改

sr1=pd.Series([12,13,14],index=['c','a','d'])
sr2=pd.Series([14,15,16],index=['d','c','a'])

#可以使用copy賦值陣列再修改
sr3=sr1[1:].copy()
print(sr3)

sr3[0]=1888
print(sr3)

'''
a    13
d    14
dtype: int64

a    1888
d      14
dtype: int64
'''

 

運算:

初始構建2個數組

sr1=pd.Series([12,13,14],index=['c','a','d'])

sr2=pd.Series([14,15,16],index=['d','c','a'])

print(sr1+sr2)
'''結果
a    29
c    27
d    28


''' 

求和運算

Pandas自動對齊功能,如果自定義了索引就會找原來索引,如果沒有值就為NaN

sr1=pd.Series([12,13,14],index=['c','a','d'])
sr3=pd.Series([11,20,10,14], index=['d','c','a','b'])
print(sr3)
#求sr1+sr3和值
print(sr1+sr3)
'''結果

a    23.0
b     NaN  #一位sr1中沒有索引b,所以顯示空
c    32.0
d    25.0

Pandas自動對齊功能,如果自定義了索引就會找原來索引,如果沒有值就為NaN

'''

 

NaN缺失資料的操作:

#先構建一個缺失資料
sr1=pd.Series([12,13,14],index=['c','a','d'])
sr2=pd.Series([14,15,16],index=['d','c','a'])

sr3=pd.Series([11,20,10,14], index=['d','c','a','b'])

#合併生成一個缺失資料
sr4=sr1+sr3
print(sr4)

'''結果

a    23.0
b     NaN
c    32.0
d    25.0
dtype: float64

'''

isnull,返回布林陣列,缺失值對應True

#isnull,返回布林陣列,缺失值對應True
res=pd.isnull(sr4)
print(res)

'''結果
a    False
b     True
c    False
d    False

'''

notnull,返回布林陣列,缺失值對應為False

#notnull,返回布林陣列,缺失值對應為False
res=pd.notnull(sr4)
print(res)
'''結果
a     True
b    False
c     True
d     True
dtype: bool

'''

dropna,過濾掉有NaN的行

#dropna,過濾掉有NaN的行
res=pd.Series.dropna(sr4)
print(res)

'''
a    23.0
c    32.0
d    25.0
dtype: float64

'''

fillna,填充缺失的資料

#fillna,填充NaN缺失的資料
res=sr4.fillna('這是給NaN做填充的資料')
print(res)

'''資料結構
a              23
b    這是給NaN做填充的資料
c              32
d              25
dtype: object

'''

 

 

DataFrame分析,待完善。。