如何按组(属性)从 python networkx 计算索引

How to calculate index from python networkx by group(attribute)

我有一个 table,其中包含 'from'、'to'、'date' 列。

我想通过 'date' 获得任何 networkx 索引(例如度、边、节点)。

现实中有很多日期,手动计算索引是不可能的。

有什么方法可以根据 'date' 计算 degree() 或 edges() 吗?

感谢阅读。

示例代码如下

df = pd.DataFrame({'from' : ['1','2','1','3'], 
                   'to' : ['3','3','2','2'], 
                   'date' : ['20200501','20200501','20200502','20200502']})

G = nx.from_pandas_edgelist(df, source = 'from', target = 'to',
                            create_using=nx.DiGraph(), edge_attr = 'date')

# It's easy to calculate any index such as 'degree','node','edge'.

G.nodes()
G.degree()
G.edge()

# However, it's not easy to calculate an index based on 'date' column.

要检查那些包含特定日期作为属性的边,迭代边,设置 data=True 并保持匹配的边。然后使用 Graph.edge_subgraph:

生成由这些边引起的新图
edges_from_date_x = [] 
some_date = '20200502'
for *edge, attr in G.edges(data=True):
    if attr['date'] == some_date:
        edges_from_date_x.append((*edge,))

print(edges_from_date_x)
# [('1', '2'), ('3', '2')]

或者,如果您更喜欢 list-comps,您可以按照@AKX 的建议进行操作:

edges_from_date_x = [(*edge,) for *edge, attr in G.edges(data=True)
                     if attr['date'] == some_date]
# [('1', '2'), ('3', '2')]

现在生成诱导子图:

# induced subgraph
G_induced = G.edge_subgraph(edges_from_date_x)
# edgelist from the induced subgraph
G_induced.edges(data=True)
#OutEdgeDataView([('1', '2', {'date': '20200502'}), ('3', '2', {'date': '20200502'})])
# same with the nodes
G.nodes()
# NodeView(('1', '3', '2'))