Pandas数据结构之Series

Series是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,python对象)

index查看series索引,values查看series值

series相比于ndarray,是一个自带索引index的数组--> 一维数组 + 对应索引

series和dict相比,series更像是一个有顺序的字典

创建方法

1.由字典创建,字典的key就是index,values就是values

dic = {'a':1 ,'b':2 , 'c':3, '4':4, '5':5}
s = pd.Series(dic)
print(s)
4    4
5    5
a    1
b    2
c    3
dtype: int64

2.由数组创建(一维数组)

arr = np.random.randn(5)
s = pd.Series(arr)
print(arr)
print(s)
# 默认index是从0开始,步长为1的数字

s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object)
print(s)
# index参数:设置index,长度保持一致
# dtype参数:设置数值类型
[ 0.11206121  0.1324684   0.59930544  0.34707543 -0.15652941]
0    0.112061
1    0.132468
2    0.599305
3    0.347075
4   -0.156529
dtype: float64
a    0.112061
b    0.132468
c    0.599305
d    0.347075
e   -0.156529
dtype: object

3. 由标量创建

s = pd.Series(10, index = range(4))
print(s)
# 如果data是标量值,则必须提供索引。该值会重复,来匹配索引的长度
0    10
1    10
2    10
3    10
dtype: int64

猜你喜欢

转载自www.cnblogs.com/nxf-rabbit75/p/9688875.html