对pandas中iloc,loc取数据差别及按条件取值的方法详解
作者:tomato_guo 时间:2021-06-15 01:58:05
Dataframe使用loc取某几行几列的数据:
print(df.loc[0:4,['item_price_level','item_sales_level','item_collected_level','item_pv_level']])
结果如下,取了index为0到4的五行四列数据。
item_price_level item_sales_level item_collected_level item_pv_level
0 3 3 4 14
1 3 3 4 14
2 3 3 4 14
3 3 3 4 14
4 3 3 4 14
而使用iloc,如下所示:
print(df.iloc[0:4,6:9])
结果如下,取得是index为0到3四行,以及第6到8列(从0列开始)3列数据。
item_price_level item_sales_level item_collected_level
0 3 3 4
1 3 3 4
2 3 3 4
3 3 3 4
另外loc可以按条件取数据:
print(df.loc[df.item_price_level==0,:])
print(df.loc[df[item_price_level]==0,:])
上面两条语句效果是一样的,都是取item_price_level为0的所有数据。可以把冒号改成几列列名,只取满足条件的某几列数据:
print(df.loc[df['item_price_level']==0,['item_price_level','item_sales_level']])
结果前两行如下:
item_price_level item_sales_level
129141 0 10
129142 0 10
条件为多个时 (同时满足两个条件如下):
print(df.loc[(item_price_level==0) & (item_sales_level==3),:])
来源:https://blog.csdn.net/qq_28811329/article/details/79961800
标签:pandas,iloc,loc
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
去掉运行JavaScript时IE产生的警告栏
2008-09-11 18:07:00
![](https://img.aspxhome.com/file/UploadPic/20089/11/2008911181137794s.png)
Python深度学习albumentations数据增强库
2023-02-14 20:00:18
![](https://img.aspxhome.com/file/2023/2/75372_0s.jpg)
ASP中汉字与UTF-8的互相转换
2009-06-24 11:15:00
Python函数高级(命名空间、作用域、装饰器)
2022-03-15 23:31:44
![](https://img.aspxhome.com/file/2023/1/75211_0s.png)
透彻掌握ASP分页技术
2009-03-09 18:26:00
python批量翻译excel表格中的英文
2022-11-16 08:09:07
官方是这样定义 DOCTYPE HTML PUBLIC 的
2007-05-31 09:43:00
python pyppeteer 破解京东滑块功能的代码
2022-10-24 06:04:10
Ajax+PHP边学边练 之五 图片处理
2023-11-18 07:21:29
![](https://img.aspxhome.com/file/2023/7/102087_0s.png)
解决安装python库时windows error5 报错的问题
2022-06-14 05:26:04
Django-xadmin+rule对象级权限的实现方式
2023-02-20 17:08:08
![](https://img.aspxhome.com/file/2023/3/77033_0s.jpg)
[图]关于网站开发中缓存 cache应用
2008-08-19 18:14:00
![](https://img.aspxhome.com/file/UploadPic/20088/19/200881918194691s.gif)
读写xml文件的2个小函数
2007-08-23 12:59:00
asp生成带有样式的word文件方法
2011-04-18 10:30:00
BootStrap 动态添加验证项和取消验证项的实现方法
2023-08-13 14:17:57
Python中的变量及简单数据类型应用
2021-06-13 09:45:26
![](https://img.aspxhome.com/file/2023/5/71365_0s.png)
对xml操作的主要方法例子
2008-06-29 14:21:00
Python中导入模块的几种方式总结
2023-08-04 17:03:54
![](https://img.aspxhome.com/file/2023/2/61432_0s.png)
Python计时相关操作详解【time,datetime】
2023-08-24 17:12:56
js如何读取csv内容拼接成json
2023-08-13 02:20:14
![](https://img.aspxhome.com/file/2023/4/56034_0s.png)