欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pandas數(shù)據(jù)類型之Series的具體使用

 更新時間:2022年08月07日 15:30:19   作者:weixin_48668114  
本文主要介紹了pandas數(shù)據(jù)類型之Series的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

pandas中包含了DataFrame和Series數(shù)據(jù)類型,分別表示二維數(shù)據(jù)結(jié)構(gòu)和一維數(shù)據(jù)結(jié)構(gòu)。
簡單的可以理解為Series為excel表的某一行或者列,DataFrame是多行多列的區(qū)域。

Series類型

  • 當(dāng)我們說excel中某一個列段的數(shù)據(jù)時(單獨(dú)的一列), 說第幾個數(shù)據(jù),我們一般會說,是第幾行的數(shù)據(jù),那么,可見雖然它是一個一維的數(shù)據(jù),但是還有索引的。
  • Series數(shù)據(jù)的默認(rèn)索引為0,1,2,3,4,5…,也稱位置索引或隱式索引。自定義索引后,稱為標(biāo)簽索引,可以用位置索引和標(biāo)簽訪問Series。

Series的三種創(chuàng)建方式

通過數(shù)組創(chuàng)建Series

import pandas as pd
import numpy as np
s1 = pd.Series([1,2,3,'tom',True])
s2 = pd.Series(range(0, 10, 1))
print(s1)
print(s2)
print(type(s1), type(s2))

創(chuàng)建指定索引列的Series

索引為數(shù)組

s1 = pd.Series([1,2], index=["a", "b"])
s2 = pd.Series(range(10,15,1), index=list('ngjur'))
s3 = pd.Series(range(100,110,2), index=range(4,9,1))
print(s1)
print(s2)
print(s3)
print(s1["a"], s1[1])    #位置索引從0開始
print(s2["r"], s2[-2])   #位置索引從0開始,可以用和列表同樣的索引訪問方式,-1表示最后一個元素
print(s3[4])    #當(dāng)定義的索引為數(shù)字時,會覆蓋之前位置索引的方式,也就是說s3[0]到s3[3],s3[-1]將不能再訪問。

a    1
b    2
dtype: int64
n    10
g    11
j    12
u    13
r    14
dtype: int64
4    100
5    102
6    104
7    106
8    108
dtype: int64
1 2
14 13
100

使用字典創(chuàng)建

key為標(biāo)簽索引,value為series的每個元素的值

s1 = pd.Series({'tom':'001', 'jack':'002'})
print(s1)

tom     001
jack    002
dtype: object

標(biāo)量創(chuàng)建Series對象

如果data是標(biāo)量值,則必須提供索引

s1 = pd.Series(5, [0, 1, 2, "a"])
print(s1[[1, "a"]])

1    5
a    5
dtype: int64

Series的常見操作

Series的值訪問

series_name[],[]內(nèi)可以為單個位置索引或者標(biāo)簽索引,也可以為位置切片或者標(biāo)簽切片,也可以為位置索引列表或者標(biāo)簽索引列表

s1 = pd.Series({'tom':'001', 'jack':'002', "Jim":"003"})
s2 = s1[["tom", "jack"]]    #使用標(biāo)簽索引列表
s3 = s1[0:3]  # 使用位置切片
s4 = s1["tom":"Jim"]    #使用標(biāo)簽切片
s5 = s1[[0,1]]
print("s1-----\n", s1["tom"], type(s1[1]))  
print("s2-----\n", s2, type(s2))  #使用標(biāo)簽索引列表
print("s3-----\n", s3, type(s3))  #使用位置切片
print("s4-----\n", s4, type(s4))  #使用標(biāo)簽切片
print("s5-----\n", s5, type(s5))  #使用位置索引列表

s1-----
 001 <class 'str'>
s2-----
 tom     001
jack    002
dtype: object <class 'pandas.core.series.Series'>
s3-----
 tom     001
jack    002
Jim     003
dtype: object <class 'pandas.core.series.Series'>
s4-----
 tom     001
jack    002
Jim     003
dtype: object <class 'pandas.core.series.Series'>
s5-----
 tom     001
jack    002
dtype: object <class 'pandas.core.series.Series'>

訪問整個series

  • series_name.values屬性
  • 返回numpy.ndarray類型
s1 = pd.Series({'tom':'001', 'jack':'002', "Jim":"003"})
s2 = s1.values
print("s2-----\n", s2, type(s2))  
s3 = pd.Series({'tom':90, 'jack':40, "Jim":100})

s2-----
 ['001' '002' '003'] <class 'numpy.ndarray'>
s2-----
 [ 90  40 100] <class 'numpy.ndarray'>

獲取索引列

series_name.index
s1 = pd.Series(['tom', 'jack', "Jim"], [90, 100, 60])
print("s1-----\n", s1, type(s1))
s1_index = s1.index
print("s1_index-----\n", s1_index, type(s1_index))
print("s1_name:", s1.name)

s1-----
 90      tom
100    jack
60      Jim
dtype: object <class 'pandas.core.series.Series'>
s1_index-----
 Int64Index([90, 100, 60], dtype='int64') <class 'pandas.core.indexes.numeric.Int64Index'>
s1_name----- None

設(shè)置名稱

如果 Series 用于生成 DataFrame,則 Series 的名稱將成為其索引或列名稱

s1 = pd.Series(np.arange(5), name='ABC',index=['a','b','c','d','e'])
print(s1)

a    0
b    1
c    2
d    3
e    4
Name: ABC, dtype: int32

Series數(shù)據(jù)編輯

Series數(shù)據(jù)刪除

使用series_name.drop(),指明index,可以為標(biāo)簽索引,或者多個標(biāo)簽索引多個組成的列表,不能為位置索引,或者切片

Series數(shù)據(jù)刪除

drop方法

s1 = pd.Series(np.arange(5), name='A',index=['a','b','c','d','e'])
print(s1)
# 單個值刪除,指明標(biāo)簽索引
s1.drop('c',inplace=False)    #inplace為False不改變原s1的內(nèi)容
print("刪除單個值,不改變s1:\n",s1)
# 多個值刪除,指明標(biāo)簽索引列表
s1.drop(['c','e'],inplace=False)

a    0
b    1
c    2
d    3
e    4
Name: A, dtype: int32
刪除單個值,不改變s1:
 a    0
b    1
c    2
d    3
e    4
Name: A, dtype: int32

a    0
b    1
d    3
Name: A, dtype: int32

# multiindex值的刪除
midx = pd.MultiIndex(levels=[['lama', 'cow', 'falcon'],
                             ['speed', 'weight', 'length']],
                     codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
                            [0, 1, 2, 0, 1, 2, 0, 1, 2]])
s1 = pd.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3],
              index=midx)
print(s1)
s1.drop(labels='weight', level=1)

lama    speed      45.0
        weight    200.0
        length      1.2
cow     speed      30.0
        weight    250.0
        length      1.5
falcon  speed     320.0
        weight      1.0
        length      0.3
dtype: float64


lama    speed      45.0
        length      1.2
cow     speed      30.0
        length      1.5
falcon  speed     320.0
        length      0.3
dtype: float64

pop方法

pop(x), 指定要pop的標(biāo)簽索引

s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
s1.pop("a")
print(s1)

b    2
c    3
dtype: int64

del方法

del s1[x], 指定要刪除的嗎標(biāo)簽索引
s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
del s1["a"]
print(s1)

b    2
c    3
dtype: int64

Series數(shù)據(jù)添加

類似于字典中元素的添加方式

s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
s1["d"] = 4
print(s1)

a    1
b    2
c    3
d    4
dtype: int64

append方法

  • Pandas Series.append()函數(shù)用于連接兩個或多個系列對象, 原對象并不改變, 這個和列表不同。
  • Series.append(to_append, ignore_index=False, verify_integrity=False)
    • to_append: 系列或系列列表/元組
    • ignore_indexd: 如果為True,則不要使用索引標(biāo)簽果為True,則在創(chuàng)建具有重復(fù)項的索引時引發(fā)異常
s1 =pd.Series(["北京", "上海", "臺灣", "香港"])
index_list =["a", "b", "c", "d"]
s1.index = index_list
print("s1-----------\n", s1)
s2 = pd.Series({"e": "廣州", "f": "深圳"})
print("s2-----------\n", s2)
s3 = s1.append(s2)
print("s3-----------\n", s3)
print(s1)
s4 = s1.append(s2, ignore_index=True)
print("s4-----------\n", s4)

s1-----------
 a    北京
b    上海
c    臺灣
d    香港
dtype: object
s2-----------
 e    廣州
f    深圳
dtype: object
s3-----------
 a    北京
b    上海
c    臺灣
d    香港
e    廣州
f    深圳
dtype: object
a    北京
b    上海
c    臺灣
d    香港
dtype: object
s4-----------
 0    北京
1    上海
2    臺灣
3    香港
4    廣州
5    深圳
dtype: object

到此這篇關(guān)于pandas數(shù)據(jù)類型之Series的具體使用的文章就介紹到這了,更多相關(guān)pandas Series內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 比較字典value的最大值的幾種方法

    python 比較字典value的最大值的幾種方法

    這篇文章主要介紹了python 比較字典value的最大值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • python雙向隊列deque的使用

    python雙向隊列deque的使用

    本文主要介紹了python雙向隊列deque的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Python制作個性化的詞云圖實(shí)例講解

    Python制作個性化的詞云圖實(shí)例講解

    大家好,本篇文章主要講的是Python制作個性化的詞云圖實(shí)例講解,感興趣的同學(xué)趕緊來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Python中可復(fù)用函數(shù)的6種實(shí)踐

    Python中可復(fù)用函數(shù)的6種實(shí)踐

    為了實(shí)現(xiàn)可維護(hù)性,我們的Python函數(shù)應(yīng)該:小型、只做一項任務(wù);沒有重復(fù);有一個層次的抽象性;有一個描述性的名字和有少于四個參數(shù),下面我們就來看看這6個特性的實(shí)踐吧
    2023-08-08
  • Python統(tǒng)計日志中每個IP出現(xiàn)次數(shù)的方法

    Python統(tǒng)計日志中每個IP出現(xiàn)次數(shù)的方法

    這篇文章主要介紹了Python統(tǒng)計日志中每個IP出現(xiàn)次數(shù)的方法,實(shí)例分析了Python基于正則表達(dá)式解析日志文件的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • 三個Python常用的數(shù)據(jù)清洗處理方式總結(jié)

    三個Python常用的數(shù)據(jù)清洗處理方式總結(jié)

    這篇文章主要為大家詳細(xì)介紹了python數(shù)據(jù)處理過程中三個主要的數(shù)據(jù)清洗說明,分別是缺失值/空格/重復(fù)值的數(shù)據(jù)清洗,感興趣的小伙伴可以了解一下
    2022-12-12
  • Python實(shí)現(xiàn)拼接多張圖片的方法

    Python實(shí)現(xiàn)拼接多張圖片的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)拼接多張圖片的方法,以具體的實(shí)際應(yīng)用引出Python拼接圖片的思路并給出了實(shí)現(xiàn)的具體方法,需要的朋友可以參考下
    2014-12-12
  • python使用pandas讀xlsx文件的實(shí)現(xiàn)

    python使用pandas讀xlsx文件的實(shí)現(xiàn)

    這篇文章主要介紹了python使用pandas讀xlsx文件的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python 模擬登陸163郵箱

    python 模擬登陸163郵箱

    這篇文章主要介紹了python 模擬登陸163郵箱的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • python?datetime模塊詳解

    python?datetime模塊詳解

    Python中常用于時間的模塊有time、datetime 和 calendar,顧名思義 time 是表示時間(時、分、秒、毫秒)等,calendar 是表示日歷時間的,本章先討論 datetime 模塊,需要的朋友可以參考下
    2022-06-06

最新評論