Home > database >  How to create a edgelist from a pandas datrafame?
How to create a edgelist from a pandas datrafame?

Time:09-14

I have a pandas dataframe (df) with 1: connected vertices and 0: unconnected vertices

X B C D
F 1 1 0
G 0 0 1
H 0 1 0

I need to convert this to a edgelist like:

Source Target
F B
F C
G D
H C

What is the best way to do this?

CodePudding user response:

You can melt, filter and cleanup:

out = (df
   .melt('X', var_name='Target')
   .loc[lambda d: d.pop('value').eq(1)]
   .rename(columns={'X': 'Source'})
 )

Output:

  Source Target
0      F      B
3      F      C
5      H      C
7      G      D

Alternative with stack:

out = (df
   .replace(0, pd.NA)
   .set_index('X')
   .stack()
   .rename_axis(['Source', 'Target'])
   .reset_index()
   .drop(columns=0)
 )

Output:

  Source Target
0      F      B
1      F      C
2      G      D
3      H      C

CodePudding user response:

Here is a way using stack() and constructing a new df.

df2 = (pd.DataFrame(
    df.set_index('X').where(lambda x: x.ne(0)).stack().index.tolist(),
    columns = ['X','Target']))

Output:

   X Target
0  F      B
1  F      C
2  G      D
3  H      C
  • Related