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

yipeiwu_com6年前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设计】。

相关文章

pytorch获取模型某一层参数名及参数值方式

1、Motivation: I wanna modify the value of some param; I wanna check the value of some param....

Python 和 JS 有哪些相同之处

【嵌牛导读】Python 是一门运用很广泛的语言,自动化脚本、爬虫,甚至在深度学习领域也都有 Python 的身影。作为一名前端开发者,也了解 ES6 中的很多特性借鉴自 Python...

Python pass详细介绍及实例代码

Python pass的用法: 空语句 do nothing 保证格式完整 保证语义完整 以if语句为例,在c或c++/Java中: if(true) ; //do...

python查询sqlite数据表的方法

本文实例讲述了python查询sqlite数据表的方法。分享给大家供大家参考。具体实现方法如下: import sqlite3 as db conn = db.connect('my...

深入浅析python 中的匿名函数

深入浅析python 中的匿名函数

定义 匿名函数指一类无须定义标识符的函数或子程序。Python用lambda语法定义匿名函数,只需用表达式而无需申明。 lambda语法的定义如下: lambda [arg1 [,a...