Node / Edge attribute filtering

11,757 views
Skip to first unread message

Lupu

unread,
Oct 26, 2010, 4:16:50 PM10/26/10
to networkx-discuss
Hi,

Does networkX contain any functions that allow you to filter a graph
based on node or edge attributes. I'm looking for something to create
a new graph with only nodes and edges of type 'X'. Also, is there a
way to select only edges of a certain weight (or range) from a
weighted graph?

Thanks,

Mike

Dan Schult

unread,
Oct 26, 2010, 6:42:44 PM10/26/10
to networkx...@googlegroups.com

To create an induced subgraph with nodes selected by some test, you
can use:

SG=G.subgraph( [n for n,attrdict in G.node.items() if attrdict
['type'] == 'X' ] )

Similarly, you can create a subgraph containing only certain edges like:

SG=networkx.Graph( [ (u,v,d) for u,v,d in G.edges(data=True) if d
['weight']>cutoff] )

These two examples use list comprehensions to create lists on the fly.
For more complicated conditions, you might need to construct a list
of nodes
or edges in a loop or in another function and then pass the list to
either
G.subgraph() for induced subgraphs given nodes; or networkx.Graph()
given edges.

Dan

Lupu

unread,
Oct 27, 2010, 8:44:31 AM10/27/10
to networkx-discuss
Thanks Dan, that was helpful.

Mike

Z Xie

unread,
Apr 30, 2016, 12:34:38 PM4/30/16
to networkx-discuss
Hi, Mike,

Have you solved the problem? 

When I apply [n for n,attrdict in G.node.items() if attrdict 
['type'] == 'X' ], it always shows error 'The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'.

You know how to address that?

All the best,
Kevin

Daniel Schult

unread,
Apr 30, 2016, 12:41:02 PM4/30/16
to networkx...@googlegroups.com
Take a look at G.node["some node"]['type'] == 'X'
Your error message suggests that this is a "Series" (probably an array of boolean values) so that Python has trouble interpreting it as a single Boolean value.

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at https://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.

Z Xie

unread,
Apr 30, 2016, 1:46:17 PM4/30/16
to networkx-discuss
I test 

stations.node['Alexandra Palace']['nlc']==6025

it shows:

579    True

Name: nlc, dtype: bool


It seems fine in this part.
If I test 
for node in stations.node:
    print type(stations.node[node]['nlc'])
it shows 

<class 'pandas.core.series.Series'>

<class 'pandas.core.series.Series'>

<class 'pandas.core.series.Series'>

<class 'pandas.core.series.Series'>

<class 'pandas.core.series.Series'>

<class 'pandas.core.series.Series'>



Do you think this is because I create nodes from a pandas dataframe?


All the best,

Kevin

sk

unread,
May 25, 2016, 10:17:47 AM5/25/16
to networkx-discuss
Kevin,

I think you are seeing the issue explained here:
http://pandas.pydata.org/pandas-docs/stable/gotchas.html

When you evaluate:
attrdict['type'] == 'X'

I suspect that attrdict is a DataFrame and it has a series in it called 'type' such that attrdict['type'] is a pandas series and so this evaluation will return an array of boolean values.

Maybe try storing the dataframe as the object, so when you add them use:
G.add_node(node_name, object=dataframe)

I am pretty sure that you can add your own keywords into the add_node call so that you could add other properties, such as:
G.add_node(node_name, object=dataframe, node_type='type1')

Hope that helps.

Z Xie

unread,
May 25, 2016, 10:23:30 AM5/25/16
to networkx-discuss
Hi SK,

Thanks for the help. I actually have solved the problem that I mixed introduce the node from dataframe and manually input. 
It seems networkx recognises the data type once a time. You either load the node from dataframe or all manually type in.

In my case, I only need to add a new dataframe contains the information I want to add.
Reply all
Reply to author
Forward
0 new messages