eleanora eleanora - 2 months ago 17
Python Question

How to create an adjacency list from an edge list efficiently

I have a csv that looks like

id1,id2
a,b
c,d
a,e
c,f
c,g


I read it in to a dataframe with df = pd.read_csv("file.csv").

I would like to convert it to an adjacency list. That is the output should be

a,b,e
c,d,f,g


I feel that df.groupby('id1') should help but variable length rows aren't suited to pandas so I am a little stuck. As my csv is large I am looking for an efficient solution however.

What is a good way to do this?

Answer

if you need CSV strings:

In [107]: df.groupby('id1').id2.apply(lambda x: ','.join(x)).reset_index()
Out[107]:
  id1    id2
0   a    b,e
1   c  d,f,g
Comments