igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Python iGraph: Create graph from numpy / pandas dataframe


From: Nick Eubank
Subject: Re: [igraph] Python iGraph: Create graph from numpy / pandas dataframe
Date: Mon, 17 Apr 2017 04:00:52 +0000

Thanks Tamas!

For anyone working on this again in the future, after writing my own iterator I suddenly discovered one is already in pandas. If your DataFrame has a structure like the one described above, then you can just use the itertupes method:

    g = ig.Graph.TupleList(df.itertuples(index=False), weights=True)

Nick



On Sat, Apr 15, 2017 at 2:35 PM Tamas Nepusz <address@hidden> wrote:
Hi,

Graph.DictList() and Graph.TupleList() are meant to be used in cases when the graph is already in memory in some format and you can cast the input data into either a list-of-dictionaries representation or a list-of-tuples representation. I believe in your case the list-of-tuples representation is more natural. So, basically, you need to figure out a way to cast a pandas DataFrame into a list (or iterable) where each element is a triplet containing the source ID, the target ID and the weight, and then call Graph.TupleList(your_list, weights=True).

All the best, 

T.

On Sat, Apr 15, 2017 at 11:30 PM, Nick Eubank <address@hidden> wrote:
Hell All,

I'm trying to figure out the most efficient way of creating an igraph object form an edgelist stored in a numpy array (or rather, technically, a pandas DataFrame). 

The DataFrame looks something like:

    a        b           weight
    100    101       1
    100    102       5
    101    103       1
    101    104       4

The numbers in a and b are NOT sequential integers, though I can write code to make those conversion if necessary. 

One option is obviously to write out the data frame as a text file, then re-import as an ncol or such. But my dataset is very large (the edgelist has about 400,000,000 entries), which means that's a massively slow process, and I think loading the text file when importing into igraph may be requiring more ram than I have. 

I've found resources on importing adjacency matrices, but nothing on edgelists. 

Anyone run into this before / have any suggestions?

Thanks!

Nick


_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help


_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help

reply via email to

[Prev in Thread] Current Thread [Next in Thread]