Pandas处理数据时的切片操作loc和iloc

loc:根据标签名字索引

iloc:根据索引进行索引

import pandas as pd


data = pd.read_csv('a.csv')
print(data)
'''
   学号  年龄   身高  体重
0   1  12  168  48
1   2  13  165  50
2   3  13  163  46
3   4  12  163  50
'''

# loc通过标签索引
d = data.loc[:, '学号']
print(d)
'''
0    1
1    2
2    3
3    4
Name: 学号, dtype: int64
'''
# loc传入索引报错
d = data.loc[:, 0]
print(d)
'''
TypeError: cannot do label indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [0] of <class 'int'>
'''

# iloc通过索引索引
d = data.iloc[:, 0]
print(d)
'''
0    1
1    2
2    3
3    4
Name: 学号, dtype: int64
'''
# iloc传入标签报错
d = data.iloc[:, '学号']
print(d)
'''
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
'''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值