简体   繁体   中英

How to retrieve row and column names from data frame?

I have generated a correlation matrix from a dataframe using pandas.corr:

cmat = sub1.corr()

cmat
Out[75]: 
          CESI001   CESI002   CESI003   CESI004
CESI001  1.000000  0.829723  0.046925  0.074475
CESI002  0.829723  1.000000  0.066766  0.073181
CESI003  0.046925  0.066766  1.000000 -0.098427
CESI004  0.074475  0.073181 -0.098427  1.000000

What I'm trying to do is generate a new dataframe consisting of [row, column, value] where the cell value meets some criteria. I have succeeded in retrieving the matching cell values:

for i2,r2 in cmat.iterrows():
for item in cmat[i2]:
    if ((item > 0.3) and (item < 0.9)):
        print (item)

This correctly produces:

0.829723365019
0.829723365019

However, I can't work backwards from there to retrieve the row and column names. I've tried .loc, .columname and several other approaches I read about here. I'm getting that Python is more about operating on the whole data frame. Any guidance appreciated.

  • stack to line up rows and columns
  • query to filter what you need

cmat.stack().to_frame('item').query('.3 < item < .9')

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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