How to create an adjacency list from an edge list efficiently
<p>I have a csv that looks like</p>

<pre><code>id1,id2
a,b
c,d
a,e
c,f
c,g
</code></pre>

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

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

<pre><code>a,b,e
c,d,f,g
</code></pre>

<p>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.</p>

<p>What is a good way to do this?</p>

<p>You can use:</p>

<pre><code>df.groupby('id1')['id2'].apply(','.join).reset_index()
</code></pre>

<p>Another solution where output is list:</p>

<pre><code>df.groupby('id1')['id2'].apply(lambda x: x.tolist())
</code></pre>

python pandas graph