简体   繁体   English

在数据框中查找特定值并记录(行、列)索引对

[英]Find Specific Values in a Dataframe and Record the (Row, Column) Index Pair

I have a Pandas data frame that looks like the following:我有一个如下所示的 Pandas 数据框:

在此处输入图像描述

Now, I want to go through the entire data frame and record the column and row index pairs (shown as just numbers for this example) for any value that is less than 0.12.现在,我想遍历整个数据框并记录任何小于 0.12 的值的列和行索引对(在此示例中仅显示为数字)。 For example, I want to identify 0.105263, record that value, and record that it is located in row 4, column 2. Are there any efficient methods for traversing a data frame and storing this kind of data in a new data frame?比如我想识别0.105263,记录那个值,记录它位于第4行第2列。有没有什么有效的方法可以遍历一个数据框,把这种数据存入一个新的数据框?

My goal is to have the output look like the following:我的目标是使输出如下所示:

Row  Col  Value
==================
4     2   0.105263
... 

You can try你可以试试

out = (df[df.le(0.12)]
       .stack().reset_index()
       .rename(columns={'level_0':'row', 'level_1':'col', 0: 'val'}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM