pandas全表查询定位某个值所在行列的方法

yipeiwu_com5年前Python基础

如下所示:

# create a dataframe with an integer feature and a categorical string feature
demo_df = pd.DataFrame({'Integer Feature': [0, 1, 2, 1], 'Categorical Feature': ['socks', 'fox', 'socks', 'box']})
demo_df

接下来用for遍历:

for indexs in demo_df.index: 
  for i in range(len(demo_df.loc[indexs].values)): 
    if(demo_df.loc[indexs].values[i] =='fox'): 
      print(indexs,i) 
      print(demo_df.loc[indexs].values[i]) 

或者用列表推导式:

Categorical FeatureInteger Feature0socks01fox12socks23box1 
[ (indexs,i) for indexs in demo_df.index for i in range(len(demo_df.loc[indexs].values)) if(demo_df.loc[indexs].values[i] =='fox')] 

以上这篇pandas全表查询定位某个值所在行列的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

ZABBIX3.2使用python脚本实现监控报表的方法

ZABBIX3.2使用python脚本实现监控报表的方法

如下所示: #!/usr/bin/python #coding:utf-8 import MySQLdb import time,datetime #zabbix数据库...

python opencv 图像拼接的实现方法

python opencv 图像拼接的实现方法

初级的图像拼接为将两幅图像简单的粘贴在一起,仅仅是图像几何空间的转移与合成,与图像内容无关。高级图像拼接也叫作基于特征匹配的图像拼接,拼接时消去两幅图像相同的部分,实现拼接合成全景图。...

Python any()函数的使用方法

描述: 如果iterable的任何元素为true,则返回true。如果iterable为空,则返回false。相当于: def any(iterable): for elemen...

python飞机大战pygame游戏框架搭建操作详解

python飞机大战pygame游戏框架搭建操作详解

本文实例讲述了python飞机大战pygame游戏框架搭建操作。分享给大家供大家参考,具体如下: 目标 明确主程序职责 实现主程序类 准备游戏精灵组 01. 明确主程序职...

python读取文件名称生成list的方法

经常需要读取某个文件夹下所有的图像文件。 我使用python写了个简单的代码,读取某个文件夹下某个后缀的文件,将文件名生成为文本(csv格式) import fnmatch impo...