利用networkx根据pandas datafram的列值创建图形

2024-04-20 08:41:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧:

import pandas as pd
df = pd.DataFrame({'id_emp': [1,2,3,4,1], 
               'name_emp': ['x','y','z','w','x'], 
               'donnated_value':[1100,11000,500,300,1000],
               'refound_value':[22000,22000,50000,450,90]
            })
df['return_percentagem'] = 100 * 
df['refound_value']/df['donnated_value']
df['classification_roi'] = ''

def comunidade(i):

    if i < 50:
        return 'Bad Investment'
    elif i >=50 and i < 100:
        return 'Median Investment'
    elif i >= 100:
        return 'Good Investment'

df['classification_roi'] = df['return_percentagem'].map(comunidade)
df

节点将是“id_emp”。如果两个节点具有相同的“id_emp”,但在“classification_roi”列中具有不同的分类,或者如果它们在“classification_roi”列中具有相同的等级,则两个节点之间将存在连接。简言之,如果节点具有相同的id,或者在“classification\u roi”列中处于同一分类中,则节点具有连接

我对networkx没有太多的实践,我所尝试的远不是理想的:

^{2}$

任何帮助都是受欢迎的。在


Tags: iddfreturn节点valuepdclassificationelif
1条回答
网友
1楼 · 发布于 2024-04-20 08:41:10

在这里,我没有使用from_pandas_edgelist。相反,请列出理解和for循环:

import matplotlib.pyplot as plt
import networkx as nx
import itertools

G = nx.Graph()

# use index to name nodes, rather than id_emp, otherwise
# multiple nodes would end up having the same name
G.add_nodes_from([a for a in df.index])

#create edges:
#same employee edges
for ie in set(df['id_emp']):
    indices = df[df['id_emp']==ie].index
    G.add_edges_from(itertools.product(indices,indices))

# same classification edges
for cr in set(df['classification_roi']):
    indices = df[df['classification_roi']==cr].index
    G.add_edges_from(itertools.product(indices,indices))

nx.draw(G)
plt.show()

enter image description here

可选:着色,以区分节点。在

^{pr2}$

enter image description here

相关问题 更多 >