广告
返回顶部
首页 > 资讯 > 后端开发 > Python >pandas数据类型之Series的具体使用
  • 712
分享到

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

2024-04-02 19:04:59 712人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录Series类型Series的三种创建方式通过数组创建Series创建指定索引列的Series使用字典创建标量创建Series对象Series的常见操作Series的值访问访问整

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

到此这篇关于pandas数据类型之Series的具体使用的文章就介绍到这了,更多相关pandas Series内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: pandas数据类型之Series的具体使用

本文链接: https://www.lsjlt.com/news/119890.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作