gpt4 book ai didi

python - 如何在python中设置Pandas DataFrame并创建networkx图

转载 作者:行者123 更新时间:2023-12-04 09:42:51 27 4
gpt4 key购买 nike

我有以下数据框:

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) 有一个 node对于每个独特的人 Person1, Person2, Person3
b) nodesizeAttempts 的总和每个人

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

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

最佳答案

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

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)

enter image description here

关于python - 如何在python中设置Pandas DataFrame并创建networkx图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62253994/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com