1

I have dataframe df_corr like this

  A2M.AX    ABC.AX    AGL.AX    AHY.AX    ALL.AX    AMC.AX    AMP.AX  
A2M.AX  1.000000 -0.505433  0.687367  0.223044 -0.664764 -0.199477   
ABC.AX -0.505433  1.000000 -0.801770 -0.606418  0.860923  0.332359   
AGL.AX  0.687367 -0.801770  1.000000  0.394378 -0.917379 -0.193461  
AHY.AX  0.223044 -0.606418  0.394378  1.000000 -0.483766 -0.063892  
ALL.AX -0.664764  0.860923 -0.917379 -0.483766  1.000000  0.177633   

i want to get the index and column names based on value This is my attempt:

df_corr[(df_corr>0.7)&(df_corr<1)]


 A2M.AX    ABC.AX    AGL.AX    AHY.AX    ALL.AX  AMC.AX    AMP.AX  

ABC.AX       NaN       NaN       NaN       NaN  0.860923     NaN  
AGL.AX       NaN       NaN       NaN       NaN       NaN     NaN        
AHY.AX       NaN       NaN       NaN       NaN       NaN     NaN       
ALL.AX       NaN  0.860923       NaN       NaN       NaN     NaN  

Expect result:

AGL.AX ALL.AX
AMC.AX ABC.AX
1
  • Is your output supposed to be an nx2 dataframe, a MultiIndex, a list-of-lists, or what? Commented Dec 11, 2018 at 23:40

2 Answers 2

3

Use stack to reshape/pivot the dataframe, and transform the index to a multiindex:

df_corr[(df_corr>0.7)&(df_corr<1)].stack()
Out[79]: 
A2M.AX        
ABC.AX  AMC.AX    0.860923
ALL.AX  AGL.AX    0.860923
dtype: float64

df_corr[(df_corr>0.7)&(df_corr<1)].stack().index.values
Out[80]: array([('ABC.AX', 'AMC.AX'), ('ALL.AX', 'AGL.AX')], dtype=object)
Sign up to request clarification or add additional context in comments.

1 Comment

Please explain this better; stack() reshapes/pivots the dataframe, and transforms the index to a multiindex e.g. ('ABC.AX', 'AMC.AX')
1

Here's one way using NumPy indexing, which avoids having to subset your dataframe.

import numpy as np

condition = df.gt(0.7) & df.lt(1)

x, y = map(list, zip(*np.where(condition.values)))

res = list(zip(df.index[x], df.columns[y]))

[('ABC.AX', 'AMC.AX'), ('ALL.AX', 'AGL.AX')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.