Home > Net >  Find Specific Values in a Dataframe and Record the (Row, Column) Index Pair
Find Specific Values in a Dataframe and Record the (Row, Column) Index Pair

Time:07-20

I have a Pandas data frame that looks like the following:

enter image description here

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. 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?

My goal is to have the output look like the following:

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

CodePudding user response:

You can try

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