pandas(一)-- Series创建及索引

  1. pandas Series 构建
  • 通过数组的方法
ar = [90.0,92.0,89.0,65.0]
s = pd.Series(ar,index=['Jack','Marry','Tom','Zack'],dtype = np.float64)
print(s)
  • 通过字典的方法
dic = {'Jack':90.0,'Marry':92.0,'Tom':89.0,'Zack':65.0}
s1 = pd.Series(dic)
print(s1)

两种方式的输出均为:

Jack     90.0
Marry    92.0
Tom      89.0
Zack     65.0
dtype: float64
  1. 查看序列的类型,索引及值

# Series 数据结构
# Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引

s = pd.Series(np.random.rand(5))
print(s)
print(type(s))
# 查看数据、数据类型

print(s.index,type(s.index))
print(s.values,type(s.values))
# .index查看series索引,类型为rangeindex
# .values查看series值,类型是ndarray

# 核心:series相比于ndarray,是一个自带索引index的数组 → 一维数组 + 对应索引
# 所以当只看series的值的时候,就是一个ndarray
# series和ndarray较相似,索引切片功能差别不大
# series和dict相比,series更像一个有顺序的字典(dict本身不存在顺序),其索引原理与字典相似(一个用key,一个用index)
  1. 直接查看索引,不能区分索引中的字符和整型数
    在原始序列的基础上,添加两行
s3['0']=100
s3['11'] =200
s2 = pd.Series(np.random.randn(3),name = 'test')
s3 = s2.rename('hehehe')
s3['0']=100
s3['11'] =200
print('******* s2 **********\n',s2,'\n','******* s3 **********\n',s3)
print('**** s2.index ****',s2.index)
print('**** s3.index ****',s3.index)

查看打印出来的结果

******* s2 **********
 0   -0.388924
1   -0.866639
2    0.045354
Name: test, dtype: float64 
 ******* s3 **********
 0      -0.388924
1      -0.866639
2       0.045354
0     100.000000
11    200.000000
Name: hehehe, dtype: float64
**** s2.index **** RangeIndex(start=0, stop=3, step=1)
**** s3.index **** Index([0, 1, 2, '0', '11'], dtype='object')

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值