pandas数据类型之Series的具体使用

发布时间:2022-08-08 10:39

pandas中包含了DataFrame和Series数据类型,分别表示二维数据结构和一维数据结构。简单的可以理解为Series为excel表的某一行或者列,DataFrame是多行多列的区域。

图片[1] - pandas数据类型之Series的具体使用 - 尘心网

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 1b 2dtype: int64n 10g 11j 12u 13r 14dtype: int644 1005 1026 1047 1068 108dtype: int641 214 13100

使用字典创建

key为标签索引,value为series的每个元素的值

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

tom 001jack 002dtype: object

标量创建Series对象

如果data是标量值,则必须提供索引

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

1 5a 5dtype: 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 001jack 002dtype: object <class 'pandas.core.series.Series'>s3-----tom 001jack 002Jim 003dtype: object <class 'pandas.core.series.Series'>s4-----tom 001jack 002Jim 003dtype: object <class 'pandas.core.series.Series'>s5-----tom 001jack 002dtype: 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 tom100 jack60 Jimdtype: 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 0b 1c 2d 3e 4Name: 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 0b 1c 2d 3e 4Name: A, dtype: int32删除单个值,不改变s1:a 0b 1c 2d 3e 4Name: A, dtype: int32

a 0b 1d 3Name: 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.0weight 200.0length 1.2cow speed 30.0weight 250.0length 1.5falcon speed 320.0weight 1.0length 0.3dtype: float64

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

pop方法

pop(x), 指定要pop的标签索引

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

b 2c 3dtype: int64

del方法

del s1[x], 指定要删除的吗标签索引
s1 = pd.Series([1, 2, 3], index=["a", "b", "c"])
del s1["a"]
print(s1)

b 2c 3dtype: int64

Series数据添加

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

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

a 1b 2c 3d 4dtype: 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: objects2-----------e 广州f 深圳dtype: objects3-----------a 北京b 上海c 台湾d 香港e 广州f 深圳dtype: objecta 北京b 上海c 台湾d 香港dtype: objects4-----------0 北京1 上海2 台湾3 香港4 广州5 深圳dtype: object

文档下载:pandas数据类型之Series的具体使用.doc文档

THE END
喜欢就支持一下吧