Pandas中Series()函数的用法

目录

pd.Series() 是 Pandas 库中用于创建系列数据的函数。Pandas 是一个强大的数据分析工具,pd.Series() 可以用来创建一个一维的带标签的数组,类似于 Python 中的列表或 NumPy 中的数组,但它可以使用标签来访问数据,而不仅仅是整数索引。

pd.Series() 的语法如下:

python 复制代码
import pandas as pd

s = pd.Series(data, index=index)

参数含义如下:

data:可以是列表、NumPy数组、字典或标量值,用于创建系列数据。

index:用于指定标签的索引,可以是列表或者与数据长度相同的其他数据结构。不指定index, 则默认index为[0,1,len(s)-1]

示例:

python 复制代码
import pandas as pd

data = [1, 2, 3, 4, 5]
s = pd.Series(data)
print(s)

0    1
1    2
2    3
3    4
4    5
dtype: int64
python 复制代码
import pandas as pd

s1=pd.Series({'a':1,'b':2,'c':3,'d':4,'e':5})
s2=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
print(s1)
print(s2)

a    1
b    2
c    3
d    4
e    5
dtype: int64
a    1
b    2
c    3
d    4
e    5
dtype: int64
相关推荐
曲幽15 分钟前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时4 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿6 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户83562907805120 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee1 天前
Python 中 `if x:` 到底在判断什么?
后端·python
OpenBayes贝式计算1 天前
解决视频模型痛点,TurboDiffusion 高效视频扩散生成系统;Google Streetview 涵盖多个国家的街景图像数据集
人工智能·深度学习·机器学习
ServBay1 天前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python