Python3.5 Pandas模塊之Series用法實例分析
本文實例講述了Python3.5 Pandas模塊之Series用法。分享給大家供大家參考,具體如下:
1、Pandas模塊引入與基本數(shù)據(jù)結(jié)構(gòu)
2、Series的創(chuàng)建
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu #模塊引入 import numpy as np import pandas as pd from pandas import Series,DataFrame #1.Series通過numpy一維數(shù)組創(chuàng)建 print("=========Series通過numpy一維數(shù)組創(chuàng)建==========") arr = np.array([1,2,3,4,5]) s1 = pd.Series(arr) print(s1) print(s1.index) print(s1.values) #2.Series直接通過一維數(shù)組創(chuàng)建 print("=========Series直接通過一維數(shù)組創(chuàng)建==========") s2 = pd.Series([10.5,20,38,40]) print(s2) #修改索引值 s2.index = ['a','b','c','d'] print(s2) #Series通過一維數(shù)組創(chuàng)建,可以在創(chuàng)建的同時自定義索引值, # 也可以之后通過賦值的形式去修改 print("=========Series創(chuàng)建的同時自定義索引值和數(shù)據(jù)類型==========") s3 = pd.Series(data=[89,78,90,87],dtype=np.float64, index=['語文','數(shù)學(xué)','英語','科學(xué)']) print(s3) #3.Series通過字典創(chuàng)建,字典的鍵對應(yīng)索引,值對應(yīng)數(shù)據(jù) print("=========Series通過字典創(chuàng)建==========") dict = {'a':1,'b':2,"c":3,"d":4} s4 = pd.Series(dict) print(s4)
運行結(jié)果:
=========Series通過numpy一維數(shù)組創(chuàng)建==========
0 1
1 2
2 3
3 4
4 5
dtype: int32
RangeIndex(start=0, stop=5, step=1)
[1 2 3 4 5]
=========Series直接通過一維數(shù)組創(chuàng)建==========
0 10.5
1 20.0
2 38.0
3 40.0
dtype: float64
a 10.5
b 20.0
c 38.0
d 40.0
dtype: float64
=========Series創(chuàng)建的同時自定義索引值和數(shù)據(jù)類型==========
語文 89.0
數(shù)學(xué) 78.0
英語 90.0
科學(xué) 87.0
dtype: float64
=========Series通過字典創(chuàng)建==========
a 1
b 2
c 3
d 4
dtype: int64
3、Series值的獲取
#模塊引入 import numpy as np import pandas as pd from pandas import Series,DataFrame #4.Series值的獲取 print("=========Series值的獲取==========") s2 = pd.Series([10.5,20,38,40]) #修改索引值 s2.index = ['a','b','c','d'] print(s2) print(s2[0]) #方括號+下標(biāo)值的形式獲取Series值 print(s2["a"]) #方括號+索引的形式獲取Series值
運行結(jié)果:
=========Series值的獲取==========
a 10.5
b 20.0
c 38.0
d 40.0
dtype: float64
10.5
10.5
4、Series運算
#模塊引入 import numpy as np import pandas as pd from pandas import Series,DataFrame #5.Series值的運算 #Series中元素級別的運算結(jié)果,包含索引值并且鍵值關(guān)系保持不變 print("=========Series值的運算==========") s6 = pd.Series({'a':1,'b':2,"c":3,"d":4}) print(s6) print("=========打印Series大于2的值==========") print(s6[s6>2]) print("=========打印Series的值除以2==========") print(s6/2) #numpy中的通用函數(shù)在Series中也支持 s7= pd.Series([1,2,-3,-4]) print(np.exp(s7))
運行結(jié)果:
=========Series值的運算==========
a 1
b 2
c 3
d 4
dtype: int64
=========打印Series大于2的值==========
c 3
d 4
dtype: int64
=========打印Series的值除以2==========
a 0.5
b 1.0
c 1.5
d 2.0
dtype: float64
0 2.718282
1 7.389056
2 0.049787
3 0.018316
dtype: float64
5、Series缺失值檢驗
#模塊引入 import numpy as np import pandas as pd from pandas import Series,DataFrame #6.Series缺失值檢驗 scores = Series({"a":88,"b":79,"c":98,"d":100}) print(scores) new = ["a","b","e","c","d"] scores = Series(scores,index=new) print(scores) print("======過濾出為缺失值的項=======") print(scores.isnull()) #NAN值返回True #print(pd.isnull(scores)) #與上面一句等價 print("======過濾出為非缺失值的項=======") print(pd.notnull(scores)) #非NAN值返回True
運行結(jié)果:
a 88
b 79
c 98
d 100
dtype: int64
a 88.0
b 79.0
e NaN
c 98.0
d 100.0
dtype: float64
======過濾出為缺失值的項=======
a False
b False
e True
c False
d False
dtype: bool
======過濾出為非缺失值的項=======
a True
b True
e False
c True
d True
dtype: bool
6、Series自動對齊
#模塊引入 import numpy as np import pandas as pd from pandas import Series,DataFrame #7.Series自動對齊 s8 = Series([12,28,46],index=["p1","p2","p3"]) s9 = Series([2,4,6,8],index=["p2","p3","p4","p5"]) print("=======s8=======") print(s8) print("=======s9=======") print(s9) print("=======s8+s9=======") print(s8+s9)
運行結(jié)果:
=======s8=======
p1 12
p2 28
p3 46
dtype: int64
=======s9=======
p2 2
p3 4
p4 6
p5 8
dtype: int64
=======s8+s9=======
p1 NaN
p2 30.0
p3 50.0
p4 NaN
p5 NaN
dtype: float64
7、Series及其索引的name屬性
#模塊引入 import numpy as np import pandas as pd from pandas import Series,DataFrame #8.Series及其name屬性 s10 = Series({"jack":18,"amy":20,"lili":23,"susan":15}) print(s10) print("=======設(shè)置name屬性后=======") s10.name = "年齡" #數(shù)據(jù)名稱標(biāo)簽 s10.index.name = "姓名" #索引名稱標(biāo)簽 print(s10)
運行結(jié)果:
amy 20
jack 18
lili 23
susan 15
dtype: int64
=======設(shè)置name屬性后=======
姓名
amy 20
jack 18
lili 23
susan 15
Name: 年齡, dtype: int64
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
caffe的python接口生成solver文件詳解學(xué)習(xí)
這篇文章主要為大家介紹了caffe的python接口生成solver文件詳解學(xué)習(xí)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06numpy數(shù)據(jù)類型dtype轉(zhuǎn)換實現(xiàn)
這篇文章主要介紹了numpy數(shù)據(jù)類型dtype轉(zhuǎn)換實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04pytorch中.numpy()、.item()、.cpu()、.detach()以及.data的使用方法
這篇文章主要給大家介紹了關(guān)于pytorch中.numpy()、.item()、.cpu()、.detach()以及.data的使用方法,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-08-08