【问题标题】:How to setup Pandas DataFrame and create networkx plot in python如何设置 Pandas DataFrame 并在 python 中创建 networkx 图
【发布时间】:2020-09-26 22:36:17
【问题描述】:

我有以下数据框:

data = [['tom', 'matt','alex',10,1,'a'], ['adam', 'matt','james',15,1,'a'],['tom', 'adam','alex',20,1,'a'],['alex', 'matt','james',12,1,'a']]
# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Person1','Person2','Person3', 'Attempts','Score','Category']) 
print(df)

  Person1 Person2 Person3  Attempts  Score Category
0     tom    matt    alex        10      1        a
1    adam    matt   james        15      1        a
2     tom    adam    alex        20      1        a
3    alex    matt   james        12      1        a

我希望创建一个网络图,其中:

a) 在Person1, Person2, Person3 中,每个独特的人都有一个node

b) nodesize 是每个人 Attempts 的总和

c) 在每个人之间有一个edge,他们共享一个Attempts,厚度是他们共享的“尝试次数”的总和。

我已通读文档,但仍在努力找出如何设置我的数据框然后进行绘图。关于如何做到这一点的任何想法?非常感谢!

【问题讨论】:

标签: python pandas networkx


【解决方案1】:

您可以从获取长度为 2combinations 开始,并使用现有的人对构建字典(将不同顺序的边的尝试添加在一起):

from itertools import combinations, chain
from collections import defaultdict

seen = set()
d = defaultdict(list)
for *people, att in df.values[:,:4].tolist():
    for edge in combinations(people, r=2):
        edge_rev = tuple(reversed(edge))
        if edge in seen:
            d[edge] += att
        elif edge_rev in seen:
            d[edge_rev] += att
        else:
            seen.add(edge)
            d[edge] = att

w_edges = ((*edge, w) for edge, w in d.items())
#('tom', 'matt', 10) ('tom', 'alex', 30) ('matt', 'alex', 22) ('adam', 'matt', 15)...

并使用add_weighted_edges_from从加权边列表构建一个图形:

G = nx.Graph()
G.add_weighted_edges_from(w_edges)

然后您可以获取图形的weights 并将它们设置为边缘宽度(按某些因素缩小):

plt.figure(figsize=(8,6))
weights = nx.get_edge_attributes(G,'weight').values()

pos = nx.circular_layout(G)
nx.draw(G, pos, 
        edge_color='lightgreen', 
        node_color='lightblue',
        width=[i/3 for i in weights],
        with_labels=True,
        node_size=1000,
        alpha=0.7)

【讨论】:

  • 这太棒了@yatu。非常感谢,我已经开始工作了,如果有任何问题,请告诉您!
  • 你知道我如何通过node_size 获取每个人尝试的总和吗?我是否需要创建另一个唯一名称列表和他们的尝试总和?谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-02-08
  • 2021-08-17
  • 1970-01-01
  • 2017-12-09
  • 2016-05-04
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多