Get column and row index pairs of Pandas DataFrame matching some criteria

╄→尐↘猪︶ㄣ 提交于 2021-02-08 04:59:50

问题


Suppose I have a Pandas DataFrame like following. These values are based on a distance matrix.

A = pd.DataFrame([(1.0,0.8,0.6708203932499369,0.6761234037828132,0.7302967433402214),
                  (0.8,1.0,0.6708203932499369,0.8451542547285166,0.9128709291752769),
        (0.6708203932499369,0.6708203932499369,1.0,0.5669467095138409,0.6123724356957946),
        (0.6761234037828132,0.8451542547285166,0.5669467095138409,1.0,0.9258200997725514),
        (0.7302967433402214,0.9128709291752769,0.6123724356957946,0.9258200997725514,1.0)
                  ])

output :

Out[65]: 
          0         1         2         3         4
0  1.000000  0.800000  0.670820  0.676123  0.730297
1  0.800000  1.000000  0.670820  0.845154  0.912871
2  0.670820  0.670820  1.000000  0.566947  0.612372
3  0.676123  0.845154  0.566947  1.000000  0.925820
4  0.730297  0.912871  0.612372  0.925820  1.000000

I want only the upper triangle.

c2 = A.copy()
c2.values[np.tril_indices_from(c2)] = np.nan

output :

Out[67]: 

        0    1        2         3         4
    0 NaN  0.8  0.67082  0.676123  0.730297
    1 NaN  NaN  0.67082  0.845154  0.912871
    2 NaN  NaN      NaN  0.566947  0.612372
    3 NaN  NaN      NaN       NaN  0.925820
    4 NaN  NaN      NaN       NaN       NaN

Now I want to get column and row index pairs based on some criteria. Eg : Get column and row indexes where value is greater than 0.8. For this the out put should be [1,3],[1,4],[3,4]. Any help on this?


回答1:


You can use numpy's argwhere:

In [11]: np.argwhere(c2 > 0.8)
Out[11]: 
array([[1, 3],
       [1, 4],
       [3, 4]])

To get the index/columns (rather than their integer locations), you could use a list comprehension:

[(c2.index[i], c2.columns[j]) for i, j in np.argwhere(c2 > 0.8)]


来源:https://stackoverflow.com/questions/22190120/get-column-and-row-index-pairs-of-pandas-dataframe-matching-some-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!