最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3.5 Pandas模塊之Series用法實例分析

 更新時間:2019年04月23日 11:26:32   作者:loveliuzz  
這篇文章主要介紹了Python3.5 Pandas模塊之Series用法,結合實例形式分析了Python3.5中Pandas模塊的Series結構原理、創(chuàng)建、獲取、運算等相關操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了Python3.5 Pandas模塊之Series用法。分享給大家供大家參考,具體如下:

1、Pandas模塊引入與基本數(shù)據(jù)結構


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ù)學','英語','科學'])
print(s3)

#3.Series通過字典創(chuàng)建,字典的鍵對應索引,值對應數(shù)據(jù)
print("=========Series通過字典創(chuàng)建==========")
dict = {'a':1,'b':2,"c":3,"d":4}
s4 = pd.Series(dict)
print(s4)

運行結果:

=========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ù)學    78.0
英語    90.0
科學    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])    #方括號+下標值的形式獲取Series值
print(s2["a"])   #方括號+索引的形式獲取Series值

運行結果:

=========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中元素級別的運算結果,包含索引值并且鍵值關系保持不變
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))

運行結果:

=========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

運行結果:

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)

運行結果:

=======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("=======設置name屬性后=======")
s10.name = "年齡"    #數(shù)據(jù)名稱標簽
s10.index.name = "姓名"    #索引名稱標簽

print(s10)

運行結果:

amy      20
jack     18
lili     23
susan    15
dtype: int64
=======設置name屬性后=======
姓名
amy      20
jack     18
lili     23
susan    15
Name: 年齡, dtype: int64

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數(shù)學運算技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

  • python?matplotlib繪圖過程中設置線條顏色實戰(zhàn)舉例

    python?matplotlib繪圖過程中設置線條顏色實戰(zhàn)舉例

    Matplotlib是一個用于數(shù)據(jù)可視化和創(chuàng)建交互式圖表的Python庫,下面這篇文章主要給大家介紹了關于python?matplotlib繪圖過程中設置線條顏色的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • selenium python瀏覽器多窗口處理代碼示例

    selenium python瀏覽器多窗口處理代碼示例

    這篇文章主要介紹了selenium python瀏覽器多窗口處理代碼示例,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • pytorch實現(xiàn)mnist分類的示例講解

    pytorch實現(xiàn)mnist分類的示例講解

    今天小編就為大家分享一篇pytorch實現(xiàn)mnist分類的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python中enumerate函數(shù)及其應用詳解

    Python中enumerate函數(shù)及其應用詳解

    在 Python 編程中,enumerate 函數(shù)是一個非常實用的工具,它能夠將一個可迭代對象組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標,這種功能在處理列表、元組、字符串等可迭代對象時非常有用,尤其是在需要同時獲取每個元素的索引和值的情況下,需要的朋友可以參考下
    2025-01-01
  • torchxrayvision包安裝過程(附pytorch1.6cpu版安裝)

    torchxrayvision包安裝過程(附pytorch1.6cpu版安裝)

    這篇文章主要介紹了torchxrayvision包安裝過程(附pytorch1.6cpu版安裝),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • numpy中的掩碼數(shù)組的使用

    numpy中的掩碼數(shù)組的使用

    本文主要介紹了numpy中的掩碼數(shù)組的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • Python Pandas實現(xiàn)DataFrame合并的圖文教程

    Python Pandas實現(xiàn)DataFrame合并的圖文教程

    我們在使用pandas處理數(shù)據(jù)的時候,往往會需要合并兩個或者多個DataFrame的操作,下面這篇文章主要給大家介紹了關于Pandas實現(xiàn)DataFrame合并的相關資料,需要的朋友可以參考下
    2022-07-07
  • python網(wǎng)絡爬蟲之模擬登錄 自動獲取cookie值 驗證碼識別的具體實現(xiàn)

    python網(wǎng)絡爬蟲之模擬登錄 自動獲取cookie值 驗證碼識別的具體實現(xiàn)

    有時,我們需要爬取一些基于個人用戶的用戶信息(需要登陸后才可以查看)就要進行模擬登陸,因為驗證碼往往是作為登陸請求中的請求參數(shù)被使用,就需要識別驗證碼
    2021-09-09
  • python 爬取國內小說網(wǎng)站

    python 爬取國內小說網(wǎng)站

    國內小說網(wǎng)站的結構,大概都如出一轍,改改地址,就差不多了,有此需求的朋友可以參考下本文的爬蟲寫法
    2021-06-06
  • python尋找list中最大值、最小值并返回其所在位置的方法

    python尋找list中最大值、最小值并返回其所在位置的方法

    今天小編就為大家分享一篇python尋找list中最大值、最小值并返回其所在位置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06

最新評論

安吉县| 清苑县| 仁化县| 德惠市| 吉隆县| 高碑店市| 来宾市| 南丹县| 洛宁县| 江永县| 綦江县| 嘉黎县| 敦煌市| 广河县| 湘潭县| 余江县| 鄄城县| 杭州市| 报价| 类乌齐县| 阿勒泰市| 文水县| 清苑县| 乐都县| 恩施市| 沁源县| 越西县| 德化县| 家居| 商河县| 安平县| 十堰市| 红桥区| 陆良县| 吉林省| 白朗县| 宣化县| 社旗县| 佛坪县| 炎陵县| 尉犁县|