Series对象本质上是一个NumPy的数组,因此NumPy的数组处理函数可以直接对Series进行处理。每个Series对象实际上都由两个数组组成,他们内部的结构很简单,由两个相互关联的数组组成,其中主数组用来存放数据。主数组的每个元素都有一个与之县关联的标签,这些标签存储在另外一个叫做Index的数组中
注意三点:
Series是一种类似于一维数组(ndarray)的对象.
数组中可存储多种数据类型.
数组中存在索引.
Series创建
列表创建
import pandas as pd
import numpy as np
from pandas import Series
s1 = Series([100, 200, 300, 400])
s1
0 100
1 200
2 300
3 400
dtype: int64
字典创建
字典的key作为索引,value作为元素
s2 = Series({'aaa': 10, 'bbb': 20, 'ccc': 30, 'ddd': 40})
s2
aaa 10
bbb 20
ccc 30
ddd 40
dtype: int64
数组创建
s3 = Series(np.arange(5))
s3
----------
0 0
1 1
2 2
3 3
4 4
数字值创建
s4 = Series(10)
s4
----------
0 10
dtype: int64
Series索引
通过index来访问Series的索引, values来访问Series的值
改变索引
s1.index = list('abcd')
s1
a 100
b 200
c 300
d 400
dtype: int64
查看索引列表
s1.index
----------
Index(['a', 'b', 'c', 'd'], dtype='object')
查看索引值列表
s1.values
----------
array([100, 200, 300, 400])
手动指定索引
series5 = Series(10, index=list('abcdef'))
series5
a 10
b 10
c 10
d 10
e 10
f 10
dtype: int64
创建时指定索引
s5 = Series([11, 22, 33, 44], index=['索引1', '索引2', '索引3', '索引4'])
s5
索引1 11
索引2 22
索引3 33
索引4 44
dtype: int64