日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

pandas數據類型之Series的具體使用_python

作者:weixin_48668114 ? 更新時間: 2022-10-02 編程語言

pandas中包含了DataFrame和Series數據類型,分別表示二維數據結構和一維數據結構。
簡單的可以理解為Series為excel表的某一行或者列,DataFrame是多行多列的區域。

Series類型

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

Series的三種創建方式

通過數組創建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))

創建指定索引列的Series

索引為數組

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])    #當定義的索引為數字時,會覆蓋之前位置索引的方式,也就是說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

使用字典創建

key為標簽索引,value為series的每個元素的值

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

tom ? ? 001
jack ? ?002
dtype: object

標量創建Series對象

如果data是標量值,則必須提供索引

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

1 ? ?5
a ? ?5
dtype: int64

Series的常見操作

Series的值訪問

series_name[],[]內可以為單個位置索引或者標簽索引,也可以為位置切片或者標簽切片,也可以為位置索引列表或者標簽索引列表

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

設置名稱

如果 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數據編輯

Series數據刪除

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

Series數據刪除

drop方法

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

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], 指定要刪除的嗎標簽索引
s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
del s1["a"]
print(s1)

b ? ?2
c ? ?3
dtype: int64

Series數據添加

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

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()函數用于連接兩個或多個系列對象, 原對象并不改變, 這個和列表不同。
  • Series.append(to_append, ignore_index=False, verify_integrity=False)
    • to_append: 系列或系列列表/元組
    • ignore_indexd: 如果為True,則不要使用索引標簽果為True,則在創建具有重復項的索引時引發異常
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

原文鏈接:https://blog.csdn.net/weixin_48668114/article/details/126199357

欄目分類
最近更新