series是一个一维数组,是基于NumPy的ndarray结构。Pandas会默然用0到n-1来作为series的index,但也可以自己指定index(可以把index理解为dict里面的key)。
Series(系列)
系列是具有均匀数据的一维数组结构。如表中系列集合:
A | B | C | D | E | F |
1 | 3 | 5 | 6 | 8 | 10 |
代码实现:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
data = np.array(['a','b','c','d'])
index = [100,101,102,103]
obj = pd.series (data,index)
print(obj)
代码中使用了padnas.Series 方法处理了一维数组,该方法共有四个参数,即:
pandas.Series(data,index,dtype,copy),各参数的说明如下:
- data:传入的数据参数,数据类型支持数组,列表等
- index:索引,与数据的长度相同
- dtype:用于表示数据类型,省略是默认为浮点数据类型
- copt:复制一份数据,默认为false
运行结果:
100 a 101 b 102 c 103 d
Series实际操作实例
1、创建一个serise数据
import pandas as pd
import numpy as np
s = pd.Series([9, 'zheng', 'beijing', 128])
print(s)
输出:
0 9 1 zheng 2 beijing 3 128 dtype: object
访问其中某个数据:
print(s[1:2])
# 打印
1 zheng
dtype: object
2、 指定index
import pandas as pd
import numpy as np
s = pd.Series([9, 'zheng', 'beijing', 128, 'usa', 990], index=[1,2,3,'e','f','g'])
print(s)
输出:
1 9
2 zheng
3 beijing
e 128
f usa
g 990
dtype: object
根据索引找出值
print(s['f']) # usa
1.3 用dictionary构造一个series
import pandas as pd
import numpy as np
s = {"ton": 20, "mary": 18, "jack": 19, "car": None}
sa = pd.Series(s, name="age")
print(sa)
打印:
car NaN
jack 19.0
mary 18.0
ton 20.0
Name: age, dtype: float64