python pandas中索引函数loc和iloc的区别分析
作者:hanyunkaka 时间:2021-08-31 21:44:21
前言
使用pandas进行数据分析的时候,我们经常需要对DataFrame的行或者列进行索引。使用pandas进行索引的方法主要有三种:直接使用行或者列标签、loc函数和iloc函数。
举个简单的例子:
import numpy as np
import pandas as pd
df = pd.DataFrame({"Fruits":["apple","pear","banana","watermelon"],"Price":[1.2,1.4,2.3,4.2],"Sales":[11,45,25,16]})
df
生成的DataFrame如下所示:
Fruits | Price | Sales | |
0 | apple | 1.2 | 11 |
1 | pear | 1.4 | 45 |
2 | banana | 2.3 | 25 |
3 | watermelon | 4.2 | 16 |
1、直接使用行或者列标签
假如我们要选取df的Fruits和Price两列,则
df[['Fruits','Price']]
Fruits | Price | |
0 | apple | 1.2 |
1 | pear | 1.4 |
2 | banana | 2.3 |
3 | watermelon | 4.2 |
假如我们要选取df的第2、3行,则
df[2:4]
Fruits | Price | Sales | |
2 | banana | 2.3 | 25 |
3 | watermelon | 4.2 | 16 |
2、loc函数
loc函数是基于行标签和列标签进行索引的,其基本用法为:
DataFrame.loc[行标签,列标签]
假如我们要选取df的第2、3行和Price、Sales对应的列,则
df[2:3,'Price':'Sales']
Price | Sales | |
2 | 2.3 | 25 |
3 | 4.2 | 16 |
假如我们要选取所有的行和Fruits、Sales对应的列,则
df.loc[:,['Fruits','Sales']]
Fruits | Sales | |
0 | apple | 11 |
1 | pear | 45 |
2 | banana | 25 |
3 | watermelon | 16 |
3、iloc函数
iloc函数是基于行和列的位置进行索引的,索引值从0开始,并且得到的结果不包括最后一个位置的值,其基本用法为:
DataFrame.iloc[行位置,列位置]
假如我们要选取df的第2、3行和第1、2列,则
df.iloc[2:4,1:3]
Price | Sales | |
2 | 2.3 | 25 |
3 | 4.2 | 16 |
假如我们要选取所有的行和第0、2列,则
df.iloc[:,[0,2]]
Fruits | Sales | |
0 | apple | 11 |
1 | pear | 45 |
2 | banana | 25 |
3 | watermelon | 16 |
来源:https://blog.csdn.net/hanyunkaka/article/details/120470899
标签:pandas,索引函数,loc
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何用ASP发送HTML格式的邮件?
2010-06-11 19:41:00
Python 中Django验证码功能的实现代码
2022-05-01 22:55:39
![](https://img.aspxhome.com/file/2023/1/66931_0s.png)
Python学习笔记之装饰器
2021-03-03 02:02:48
![](https://img.aspxhome.com/file/2023/4/92474_0s.png)
Python OpenCV阈值处理详解
2023-10-07 19:38:47
![](https://img.aspxhome.com/file/2023/2/80592_0s.png)
pytorch中可视化之hook钩子
2021-07-30 04:26:23
![](https://img.aspxhome.com/file/2023/4/89404_0s.png)
对抗MySQL数据库解密高手
2008-12-25 13:14:00
python 实现全球IP归属地查询工具
2023-10-05 16:31:33
教程javascript的function(函数)
2007-09-30 13:38:00
![](https://img.aspxhome.com/file/UploadPic/20079/30/200793013468441s.gif)
详解python-图像处理(映射变换)
2023-12-23 06:43:35
![](https://img.aspxhome.com/file/2023/9/93709_0s.png)
Python真题案例之二分法查找详解
2023-09-23 01:39:07
![](https://img.aspxhome.com/file/2023/6/85376_0s.png)
python通过对字典的排序,对json字段进行排序的实例
2023-06-15 02:20:40
php+mysql开发的最简单在线题库(在线做题系统)完整案例
2023-08-21 20:03:14
![](https://img.aspxhome.com/file/2023/3/553631_0s.png)
Python线程指南分享
2023-01-13 15:33:58
![](https://img.aspxhome.com/file/2023/9/86329_0s.jpg)
pycharm运行出现ImportError:No module named的解决方法
2022-09-10 18:06:23
![](https://img.aspxhome.com/file/2023/1/81861_0s.jpg)
如何创建SQL Server 2000故障转移群集
2009-02-13 17:18:00
python向字符串中添加元素的实例方法
2023-08-24 00:59:21
![](https://img.aspxhome.com/file/2023/4/97284_0s.jpg)
Doctype之谜
2009-07-22 20:48:00
DBA经验:如何进行MySQL数据库表的故障检测
2009-02-12 17:37:00
作符合用户体验的input输入框
2008-01-10 12:21:00
python爬取豆瓣电影排行榜(requests)的示例代码
2022-10-16 02:18:46
![](https://img.aspxhome.com/file/2023/9/86249_0s.png)