脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pandas数据类型之Series的具体使用

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

2022-08-07 19:18weixin_48668114 Python

本文主要介绍了pandas数据类型之Series的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

Series类型

  • 当我们说excel中某一个列段的数据时(单独的一列), 说第几个数据,我们一般会说,是第几行的数据,那么,可见虽然它是一个一维的数据,但是还有索引的。
  • Series数据的默认索引为0,1,2,3,4,5…,也称位置索引或隐式索引。自定义索引后,称为标签索引,可以用位置索引和标签访问Series。

Series的三种创建方式

通过数组创建Series

?
1
2
3
4
5
6
7
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

索引为数组

?
1
2
3
4
5
6
7
8
9
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的每个元素的值

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

tom     001
jack    002
dtype: object

标量创建Series对象

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

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

1    5
a    5
dtype: int64

Series的常见操作

Series的值访问

series_name[],[]内可以为单个位置索引或者标签索引,也可以为位置切片或者标签切片,也可以为位置索引列表或者标签索引列表

?
1
2
3
4
5
6
7
8
9
10
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类型
?
1
2
3
4
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'>

获取索引列

?
1
2
3
4
5
6
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 的名称将成为其索引或列名称

?
1
2
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方法

?
1
2
3
4
5
6
7
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

?
1
2
3
4
5
6
7
8
9
# 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的标签索引

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

b    2
c    3
dtype: int64

del方法

?
1
2
3
4
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数据添加

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

?
1
2
3
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,则在创建具有重复项的索引时引发异常
?
1
2
3
4
5
6
7
8
9
10
11
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

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

延伸 · 阅读

精彩推荐
  • Pythonpython文件的md5加密方法

    python文件的md5加密方法

    这篇文章主要介绍了python文件的md5加密方法,涉及Python针对文件的读取与字符串加密的相关技巧,需要的朋友可以参考下 ...

    火星大熊猫7522020-08-18
  • Pythonpython实现selenium网络爬虫的方法小结

    python实现selenium网络爬虫的方法小结

    这篇文章主要介绍了python实现selenium网络爬虫的方法小结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考...

    不想打代码了9042021-09-19
  • PythonPython字符串拼接六种方法介绍

    Python字符串拼接六种方法介绍

    这篇文章主要介绍了Python字符串拼接六种方法介绍,具有一定借鉴价值,需要的朋友看可以参考下。...

    Joe_Z9742020-12-26
  • Python浅谈对Python变量的一些认识理解

    浅谈对Python变量的一些认识理解

    变量(variable)是编程的基础概念,Python 的变量看似简单,深入了解却不易.文中有非常详细的介绍及代码示例,对正在学习python的小伙伴们很有帮助,需要的朋...

    软件技术学习开发爱好者5992021-11-03
  • Pythonpython使用多线程备份数据库的步骤

    python使用多线程备份数据库的步骤

    在日常服务器运维工作中,备份数据库是必不可少的,刚工作那会看到公司都是用shell脚本循环备份数据库,到现在自己学习python语言后,利用多进程多线...

    Lucky_Tomato5572021-11-17
  • Pythondjango利用request id便于定位及给日志加上request_id

    django利用request id便于定位及给日志加上request_id

    这篇文章主要介绍了django利用request id便于定位及给日志加上request_id的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用django具有一定的...

    carey4492021-03-29
  • PythonPython中的for循环详情

    Python中的for循环详情

    这篇文章主要介绍了Python中的for循环,for语句实际上解决的是循环问题。在很多的高级语言中都有for循环(for loop)。下面就来简单给大家介绍Python中的f...

    尤而小屋3952022-01-24
  • Python解决Django的request.POST获取不到内容的问题

    解决Django的request.POST获取不到内容的问题

    今天小编就为大家分享一篇解决Django的request.POST获取不到内容的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    NoneSec12202021-02-26