如何在python中获取 pandas 的x和y值对 [英] How to get x and y value pairs of pandas in python

查看:193
本文介绍了如何在python中获取 pandas 的x和y值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用大熊猫创建了一个共现矩阵,如下所示.

I have created a co-occurrence matrix as follows using pandas.

import pandas as pd
import numpy as np

lst = [
    ['a', 'b'],
    ['b', 'c', 'd', 'e', 'e'],
    ['a', 'd', 'e'],
    ['b', 'e']
]

u = (pd.get_dummies(pd.DataFrame(lst), prefix='', prefix_sep='')
       .groupby(level=0, axis=1)
       .sum())

v = u.T.dot(u)
v.values[(np.r_[:len(v)], ) * 2] = 0
print(v)

输出如下.

   a  b  c  d  e
a  0  1  0  1  1
b  1  0  1  1  3
c  0  1  0  1  2
d  1  1  1  0  3
e  1  3  2  3  0

我想将上述数据帧转换为(x,y)对.如您所见,输出矩阵是对称的(即对角线的上部和对角线的下部相似).因此,我很高兴只从其中一部分中获得(x,y)对(例如,仅使用上部).

I would like to convert the above mentioned dataframe into (x,y) pairs. As you can see the output matrix is symmetric (i.e the upper part from the diagonal and lower part from the diagonal is similar). Therefore, I am happy to only get the (x,y) pairs from one part of them (e.g., only using upper part).

因此,在上面的矩阵中,输出应该是(即(x,y)对,其值大于零>0);

So, in the above matrix the ouput should be (i.e. (x,y) pairs whose value is greater than zero >0);

[('a','b'), ('a', 'd'), ('a','e'), ('b', 'c'), ('b', 'd'), ('b', 'e'), 
('c', 'd'), ('c', 'e'), ('d', 'e')]

是否可以在熊猫中执行此操作?

Is it possible to perform this in pandas?

如果需要,我很乐意提供更多详细信息.

I am happy to provide more details if needed.

推荐答案

您可以尝试 np.where :

arr = np.where(v>=1)
corrs = [(v.index[x], v.columns[y]) for x, y in zip(*arr)]
corrs

[('a', 'b'),
 ('a', 'd'),
 ('a', 'e'),
 ('b', 'a'),
 ('b', 'c'),
 ('b', 'd'),
 ('b', 'e'),
 ('c', 'b'),
 ('c', 'd'),
 ('c', 'e'),
 ('d', 'a'),
 ('d', 'b'),
 ('d', 'c'),
 ('d', 'e'),
 ('e', 'a'),
 ('e', 'b'),
 ('e', 'c'),
 ('e', 'd')]

然后您可以过滤列表:

final_arr = []
for x, y in corrs:
    if (y,x) not in final_arr:
        final_arr.append((x,y))
final_arr

[('a', 'b'),
 ('a', 'd'),
 ('a', 'e'),
 ('b', 'c'),
 ('b', 'd'),
 ('b', 'e'),
 ('c', 'd'),
 ('c', 'e'),
 ('d', 'e')]

这篇关于如何在python中获取 pandas 的x和y值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆