Series
Series
对应一维数组的数据结构,可以保存任何类型的数据
Series的创建
# 导入pandas
import pandas as pd
pd.Series(data=None, index=None, dtype=None)
- 参数
- data: 传入的数据,可以是numpy、list等
- index: 索引,必须唯一,且与数据的长度相等。如果为空,则默认会自动创建一个从0到N的整数索引
- dtype: 数据的类型
通过已有数据创建
- 指定内容,默认索引
>>> pd.Series(np.arange(9))
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
dtype: int32
- 指定索引
>>> pd.Series([1.2, 2.3, 4.5, 5.0], index=[1, 2, 3, 4])
1 1.2
2 2.3
3 4.5
4 5.0
dtype: float64
通过字典数据创建
>>> color_count = pd.Series({'red':10, 'green':20, 'blue':100})
>>> color_count
red 10
green 20
blue 100
dtype: int64
Series的属性
- index
- values
- 使用索引获取数据
>>> color_count.index
Index(['red', 'green', 'blue'], dtype='object')
>>> color_count.values
array([ 10, 20, 100], dtype=int64)
>>> color_count[0]
10