从网络中的数据帧向节点添加属性 [英] Adding Attributes to Nodes from Dataframe in a Network
问题描述
我之前问过这个问题,但在评论后我想更改它:
I asked this question before but after a comment I want to change it:
import networkx as nx
我所拥有的: 使用 nx.from_pandas_dataframe()
问题:当我使用 nx.from_pandas_dataframe(dataframe, 'Sources', 'Target', edge_attr=True)
nx 认为所有其他列而不是 Sources 和 Target 是边缘的属性.
Problem : When I used nx.from_pandas_dataframe(dataframe, 'Sources', 'Target', edge_attr=True)
nx thinks all other colums rather then Sources and Target are attributes to edges.
我想做什么:我想使用数据框的一些列作为节点的属性.
What I want to do: I want to use some columns of dataframe as attributes of nodes.
我的问题:有什么简单的方法可以做到这一点(比如 edge_attr=True
只用一个函数添加许多属性)?如果不是,如何从数据帧向节点添加许多属性?例如:
My Question : Is there any easy way to do this(like edge_attr=True
add many attributes with only one function)? If not How can I add many attributes to nodes from a dataframe?
For example:
Airline Sources Target SLongitudeLatitude TLongitudeLatitude SPopulation NPopulation
Alpha A Z 1 10 101 111
Delta B Y 2 11 102 112
Gamma C X 3 12 103 113
Omega D W 4 13 104 114
Lambda E U 5 14 105 115
Zeta F T 6 15 106 116
Beta G S 7 16 107 117
Phi H R 8 17 108 118
Airline 列是边缘的属性,但其余列(SLongitudeLatitude、TLongitudeLatitude 和 Population)是节点的属性.
Airline Column is attribute to edges but rest of columns (SLongitudeLatitude, TLongitudeLatitude and Population) are attribute for nodes.
或者我可以为来自不同数据帧的节点添加属性吗?
Or can I add attributes to nodes from different dataframe?
推荐答案
Networkx 只能从 Pandas 数据帧中获取边缘属性,但在单独的步骤中添加节点属性也不错,因为这个答案概述:
Networkx can only take edge attributes from Pandas dataframes, but it isn't too bad to add the node attributes in a separate step, as this answer outlines:
这篇关于从网络中的数据帧向节点添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!