从 Pandas 数据帧向 networkx 网络添加属性

问题描述

我有一个 .gml 文件,如下所示:

graph [
  directed 1
  node [
    id 0
    label "5B5F2C071D12AF13219DF5EBE05132AF"
  ]
  node [
    id 1
    label "9FB3B96B6D5E16C9DD564AA3E84F1954"
  ]
  node [
    id 2
    label "B3299B0E587D7275E3E4D530E9EECF50"
  ]
  node [
    id 3
    label "6432F1DF21BA38368D9A165C739EEBB3"
  ]
  node [
    id 4
    label "85D5C50A6D882CA8E4BB00BCA3574417"
  ]
  node [
    id 5
    label "0D8583F810B9720A8032BB939F12B3FF"
  ]
  node [
    id 6
    label "6C10A9E9F325CAA3CCB7F9A0D6983D2A"
  ]
  node [
    id 7
    label "B0C50ED1DEC9E06E4C64E7419DDC4B09"
  ]
]

我是这样读这个文件的:

import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd

G = nx.read_gml(path)
list(G.nodes(data=True))

我有一个如下所示的熊猫数据框:

df = pd.DataFrame({'USER': ['5B5F2C071D12AF13219DF5EBE05132AF','9FB3B96B6D5E16C9DD564AA3E84F1954','B3299B0E587D7275E3E4D530E9EECF50','6432F1DF21BA38368D9A165C739EEBB3','85D5C50A6D882CA8E4BB00BCA3574417','85D5C50A6D882CA8E4BB00BCA3574417'],'CLASSE': [3,3,2,1,1]})
df

我需要在两个数据集之间进行合并,以便将上面呈现的数据帧的 CLASSE 属性添加到网络中,使网络看起来像以下演示:

graph [
  directed 1
  node [
    id 0
    label "5B5F2C071D12AF13219DF5EBE05132AF"
    CLASSE 3
  ]
...others nodes

我需要这样做是因为我想过滤网络上的 CLASSE 属性。有人可以帮忙吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)