Pandas中创建Series方法有哪些?

Python (167) 2023-07-13 14:08:57

在python的Pandas库中,作为其中一个数据结构的Series,虽然是一维的,但能够存储不同类型的数据,本文介绍Pandas中创建Series的三种方法:1、使用一个列表生成一个Series;2、使用一个字典生成Series,其中字典的键,就是索引;3、使用数组生成一个Series。

1、使用一个列表生成一个Series

myList=['a','b','c','d']#创建一个列表
s1=pd.Series(data=myList)
print(s1)
--------------------------------
输出:
0a
1b
2c
3d
dtype:object

2、使用一个字典生成Series,其中字典的键,就是索引

dictionary1={"name":"nick","age":12,"sex":"male"}

s5=pd.Series(dictionary1)
print(s5)

3、使用数组生成一个Series

importnumpyasnp
arrySer=pd.Series(np.arange(10,15),index=['a','b','c','d','e'])
print(arrySer)

以上就在python的Pandas库中创建Series的三种方法,大家选择其中一种方法使用就可以啦~

THE END

发表回复