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

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

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

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

Series類型

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

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

通過(guò)數(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開始,可以用和列表同樣的索引訪問(wèn)方式,-1表示最后一個(gè)元素
print(s3[4])    #當(dāng)定義的索引為數(shù)字時(shí),會(huì)覆蓋之前位置索引的方式,也就是說(shuō)s3[0]到s3[3],s3[-1]將不能再訪問(wèn)。

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的每個(gè)元素的值

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

tom     001
jack    002
dtype: object

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

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

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

1    5
a    5
dtype: int64

Series的常見操作

Series的值訪問(wèn)

series_name[],[]內(nèi)可以為單個(gè)位置索引或者標(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'>

訪問(wèn)整個(gè)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)簽索引,或者多個(gè)標(biāo)簽索引多個(gè)組成的列表,不能為位置索引,或者切片

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

drop方法

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

a    0
b    1
c    2
d    3
e    4
Name: A, dtype: int32
刪除單個(gè)值,不改變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], 指定要?jiǎng)h除的嗎標(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ù)用于連接兩個(gè)或多個(gè)系列對(duì)象, 原對(duì)象并不改變, 這個(gè)和列表不同。
  • Series.append(to_append, ignore_index=False, verify_integrity=False)
    • to_append: 系列或系列列表/元組
    • ignore_indexd: 如果為True,則不要使用索引標(biāo)簽果為True,則在創(chuàng)建具有重復(fù)項(xiàng)的索引時(shí)引發(fā)異常
s1 =pd.Series(["北京", "上海", "臺(tái)灣", "香港"])
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    臺(tái)灣
d    香港
dtype: object
s2-----------
 e    廣州
f    深圳
dtype: object
s3-----------
 a    北京
b    上海
c    臺(tái)灣
d    香港
e    廣州
f    深圳
dtype: object
a    北京
b    上海
c    臺(tái)灣
d    香港
dtype: object
s4-----------
 0    北京
1    上海
2    臺(tái)灣
3    香港
4    廣州
5    深圳
dtype: object

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

相關(guān)文章

最新評(píng)論

乌鲁木齐市| 汕尾市| 香河县| 上林县| 古浪县| 海原县| 峡江县| 肃宁县| 上林县| 偏关县| 大冶市| 准格尔旗| 武威市| 海宁市| 神池县| 威海市| 三明市| 汝城县| 井研县| 顺平县| 满洲里市| 广丰县| 荣成市| 滨州市| 嫩江县| 黎川县| 内乡县| 东乡| 巍山| 通州区| 贡山| 桃江县| 濮阳市| 全州县| 三河市| 榆林市| 夹江县| 无为县| 宝清县| 嘉黎县| 贵州省|