Python 删除权重在networkx中特定范围内的边

Python 删除权重在networkx中特定范围内的边,python,pandas,networkx,Python,Pandas,Networkx,我有一个数据帧。我把那个数据框转换成了一个图表。之后,我想删除特定重量范围内的边缘: 列: df.columns: Index(['source', 'target', 'weight'], dtype='object') len(df) 1048575 df.dtypes source int64 target float64 weight int64 dtype: object Graphtype = nx.Graph() G = nx.from_pandas

我有一个数据帧。我把那个数据框转换成了一个图表。之后,我想删除特定重量范围内的边缘:

列:

df.columns:
Index(['source', 'target', 'weight'], dtype='object')
len(df)
1048575
df.dtypes
source      int64
target    float64
weight      int64
dtype: object
Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)
print(nx.info(G))
Name: 
Type: Graph
Number of nodes: 609627
Number of edges: 915549
Average degree:   3.0036
长度:

df.columns:
Index(['source', 'target', 'weight'], dtype='object')
len(df)
1048575
df.dtypes
source      int64
target    float64
weight      int64
dtype: object
Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)
print(nx.info(G))
Name: 
Type: Graph
Number of nodes: 609627
Number of edges: 915549
Average degree:   3.0036
数据类型:

df.columns:
Index(['source', 'target', 'weight'], dtype='object')
len(df)
1048575
df.dtypes
source      int64
target    float64
weight      int64
dtype: object
Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)
print(nx.info(G))
Name: 
Type: Graph
Number of nodes: 609627
Number of edges: 915549
Average degree:   3.0036
现在,构建networkx图形:

df.columns:
Index(['source', 'target', 'weight'], dtype='object')
len(df)
1048575
df.dtypes
source      int64
target    float64
weight      int64
dtype: object
Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)
print(nx.info(G))
Name: 
Type: Graph
Number of nodes: 609627
Number of edges: 915549
Average degree:   3.0036
图形信息:

df.columns:
Index(['source', 'target', 'weight'], dtype='object')
len(df)
1048575
df.dtypes
source      int64
target    float64
weight      int64
dtype: object
Graphtype = nx.Graph()
G = nx.from_pandas_edgelist(df, 'source','target', edge_attr='weight', create_using=Graphtype)
print(nx.info(G))
Name: 
Type: Graph
Number of nodes: 609627
Number of edges: 915549
Average degree:   3.0036

degrees = sorted(G.degree, key=lambda x: x[1], reverse=True)
degrees
[(a, 1111),
 (c, 1107),
 (f, 836),
 (g, 722),
 (h, 608),
 (k, 600),
 (r, 582),
 (z, 557),
 (l, 417), etc....
我想做的是删除具有特定权重的边。例如,我希望删除权重>=500的所有边

to_remove = [(a,b) for a,b in G.edges(data=True) if "weight" >= 500]
G.remove_edges_from(to_remove)
但是,我收到以下错误消息:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-df3a67f18df9> in <module>()
----> 1 to_remove = [(a,b) for a,b in G.edges(data=True) if "weight" >=500]

<ipython-input-12-df3a67f18df9> in <listcomp>(.0)
----> 1 to_remove = [(a,b) for a,b in G.edges(data=True) if "weight" >=500]

ValueError: too many values to unpack (expected 2)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1 to_remove=[(a,b)对于G.边中的a,b(数据=真),如果“权重”>=500]
英寸(.0)
---->1 to_remove=[(a,b)对于G.边中的a,b(数据=真),如果“权重”>=500]
ValueError:要解压缩的值太多(应为2个)
你知道我为什么会收到这个信息吗?或者也许有更好的方法做到这一点?
谢谢

我就是这样解决这个问题的:

threshold = 500

# filter out all edges above threshold and grab id's
long_edges = list(filter(lambda e: e[2] > threshold, (e for e in G.edges.data('weight'))))
le_ids = list(e[:2] for e in long_edges)

# remove filtered edges from graph G
G.remove_edges_from(le_ids)

我就是这样解决这个问题的:

threshold = 500

# filter out all edges above threshold and grab id's
long_edges = list(filter(lambda e: e[2] > threshold, (e for e in G.edges.data('weight'))))
le_ids = list(e[:2] for e in long_edges)

# remove filtered edges from graph G
G.remove_edges_from(le_ids)

在这种理解中,您将字符串“weight”与int进行比较,这没有多大意义:

[(a,b) for a,b in G.edges(data=True) if "weight" >= 500]
另外,是什么导致了异常,如果传递
data=True
,将得到一个3元组,其中第三个元素是属性的dict

您可能想做的是:

[(a,b) for a, b, attrs in G.edges(data=True) if attrs["weight"] >= 500]

在这种理解中,您将字符串“weight”与int进行比较,这没有多大意义:

[(a,b) for a,b in G.edges(data=True) if "weight" >= 500]
另外,是什么导致了异常,如果传递
data=True
,将得到一个3元组,其中第三个元素是属性的dict

您可能想做的是:

[(a,b) for a, b, attrs in G.edges(data=True) if attrs["weight"] >= 500]