码迷,mamicode.com
首页 > 其他好文 > 详细

Pandas数据结构之Series

时间:2018-09-22 00:46:07      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:pre   数组   type   整数   设置   自带   python对象   类型   开始   

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

Pandas数据结构之Series

标签:pre   数组   type   整数   设置   自带   python对象   类型   开始   

原文地址:https://www.cnblogs.com/nxf-rabbit75/p/9688875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!