python中loc方法和iloc方法

pandas以类似字典的方式来获取某一列的值

loc是指location的意思,iloc中的i是指integer。这两者的区别如下:

  • loc works on labels in the index. loc对索引中的标签起作用。
  • iloc works on the positions in the index (so it only takes integers) iloc在索引中的位置上作用(所以它只接受整数)。

1.loc方法

通过标签索引行的数据

2.iloc方法

通过内置位置index索引行的数据

3.代码

s = Series(np.random.randint(0,150,size=6),
           index=[['a','b','c','d','e','f']])
print(s)

print("*****")
# loc方法取值
print(s.loc['a'])
print(s.loc[['a','b']])

print("*****")
# iloc方法取值
print(s.iloc[0])
print(s.iloc[5])

五行结果分别为

a     16
b     84
c     36
d     81
e     66
f    125
dtype: int32
*****
a    16
dtype: int32
a    16
b    84
dtype: int32
*****
16
125

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值