具有累积和的边缘上的Networkx权重

时间:2017-01-06 03:43:01

标签: python pandas networkx

我正在绘制一个带边缘权重的网络图,我想累计加权。下面的代码只获取边的最后权重,但累计和。有5个节点和3个边缘。边缘为('C', 'E')[1, 1, 1],权重为[2, 2, 1]。我希望权重为[1, 1, 1]而不是import pandas as pd import networkx as nx ints = [1] * 5 a = ['A', 'B', 'C', 'A', 'A'] b = ['D', 'A', 'E', 'D', 'B'] df = pd.DataFrame(ints, columns=['weight']) df['a'] = a df['b'] = b df weight a b 0 1 A D 1 1 B A 2 1 C E 3 1 A D 4 1 A B 。需要帮忙。 TKS。

1。创建一个df

G=nx.from_pandas_dataframe(df, 'a', 'b', ['weight'])
edges = G.edges() 
weights = [G[u][v]['weight'] for u,v in edges]
pos = nx.circular_layout(G)
nx.draw(G, pos, with_labels=True,  width=weights)

2。绘制图表

parse

enter image description here

1 个答案:

答案 0 :(得分:1)

所以,我认为你不能直接用nx.from_pandas_dataframe做累积总和,至少在快速查看文档之后。

但是,在将pandas传递给networkx之前,您可以在[2,2,1]中执行此操作。

由于您说您希望权重为df["a'"] = pd.DataFrame([df["a"], df["b"]]).min() df["b'"] = pd.DataFrame([df["a"], df["b"]]).max() ,这意味着您将图表视为无向,因此您必须首先确保权重:

groupby

然后你可以用简单的df = df.groupby(by = ["a'", "b'"]).sum().reset_index()

来做累积和
df

此时,nx.from_pandas_dataframe将正确转换G = nx.from_pandas_dataframe(df, "a'", "b'", ['weight']) [G[u][v]['weight'] for u,v in edges]

class SfclReport extends Model
    {
   public $fy_yr;
   public $fy_qtr;

   public function rules()
    {
        return [
            [['fy_id', 'fy_qtr'], 'required'],
            [['fy_id',], 'integer'],
            [['fy_yr','fy_qtr'], 'string'],

        ];
    }


}