如何设置 Pandas DataFrame 并在 python 中创建 networkx 图
How to setup Pandas DataFrame and create networkx plot in python
我有以下数据框:
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
,厚度是他们共享的`尝试的总和。
我已经通读了文档,但仍在努力寻找如何设置我的数据框然后进行绘图。关于如何做到这一点的任何想法?非常感谢!
你可以从获取长度二开始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)...
从加权边列表构建一个图
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)
我有以下数据框:
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
,厚度是他们共享的`尝试的总和。
我已经通读了文档,但仍在努力寻找如何设置我的数据框然后进行绘图。关于如何做到这一点的任何想法?非常感谢!
你可以从获取长度二开始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)...
从加权边列表构建一个图
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)