pandas的数据结构介绍(一)—— Series

pandas两个主要数据结构之一——Series

  • 类似于一维数组,由一组数据和与其相关的一组索引组成
obj = Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c']) 
print(obj)
'''
d    4
b    7
a   -5
c    3
dtype: int64
'''
  • 可通过索引,选取单个或多个值
tmp = ['a', 'b']
print(obj[tmp])
"""
a   -5
b    7
dtype: int64
"""
  • 也可根据布尔型数组进行运算
print(obj[obj > 0])
print(obj*2)
"""
d    4
b    7
c    3
dtype: int64
d     8
b    14
a   -10
c     6
dtype: int64
"""
  • 还可以看作是定长的有序字典
data = {'a': 1, 'b': 2, 'c': 3, 'd': -1}
obj2 = Series(data)  # 通过字典直接生成Series
print(obj2)
"""
a    1
b    2
c    3
d   -1
dtype: int64
"""

t = 'a' in obj2  # 判断‘a’是否为obj2索引
print(t)
"""
True
"""
  • 生成Series时,无对应值自动填充为 NaN,且Series对数据会根据索引自动对齐
# 如果 obj2 = Series(data, index = ...) 中,index对应无对应值,则其填充为NaN
index = ['a', 'e', 'b', 'c', 'd']  # 多了一个‘e’,并且位置不同(在生成时会自动对齐)
obj3 = Series(data, index=index)
print(obj3)
"""
a    1.0
e    NaN
b    2.0
c    3.0
d   -1.0
dtype: float64
"""

# 数据是否缺失可用isnull检测
print(obj3.isnull())
"""
a    False
e     True
b    False
c    False
d    False
dtype: bool
"""
  • Series可进行运算,不过与NaN运算结果始终为NaN
obj4 = Series({'a': 1, 'b': 2, 'd': -1, 'e': 5})
print(obj4)
print(obj3+obj4)
"""
a    1
b    2
d   -1
e    5
dtype: int64

# obj4 中无‘c’索引,其默认为NaN
# 运算完后会自动排序
a    2.0
b    4.0
c    NaN
d   -2.0
e    NaN
dtype: float64
"""
  • Series本身及其索引均有一个name属性
obj4.name = 'obj4'
obj4.index.name = 'index'
print(obj4)
"""
index
a    1
b    2
d   -1
e    5
Name: obj4, dtype: int64
"""
  • Series索引可通过赋值方式就地修改
obj4.index = [1, 2, 3, 4] # 索引个数要相同,且更改后索引名会清空
print(obj4)
"""
1    1
2    2
3   -1
4    5
Name: obj4, dtype: int64
"""
posted @   solmp  阅读(100)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示