简体   繁体   中英

Create a direct edge matrix from edge list

Sample data:

data = { 
    'node1': [1, 1,1, 2,2,5],
 'node2': [8,16,22,5,25,10],
 'weight': [1,1,1,1,1], }
df = pd.DataFrame(data, columns = ['node1','node2','weight'])

The given data is edge list with first column indicating the node 1 and the second column shows the node which is directly connected to the first node. Given is a edge list with column 1 as node1 , column 2 as node2 and weight. I want to create a matrix with each row representing all the direct edges there are for a given node. (Each row is a node and the columns in it are the direct edges for the given node) using Pandas Dataframe.

output:

8   16  22

5   25  0

0   0   0

0   0   0

10  0   0

IIUC

df=df.assign(Cu=df.groupby('node1').cumcount()).set_index('Cu').groupby('node1').apply(lambda x : x['node2']*x['weight']).unstack('Cu').fillna(0)
df
Out[71]: 
Cu        0     1     2
node1                  
1       8.0  16.0  22.0
2       5.0  25.0   0.0
5      10.0   0.0   0.0

For getting you out put , you can reindex + fillna

Edit: Notice your expected output contian some all 0 row,

df.reindex([1,2,3,4,5]).fillna(0)
Out[107]: 
Cu        0     1     2
node1                  
1       8.0  16.0  22.0
2       5.0  25.0   0.0
3       0.0   0.0   0.0
4       0.0   0.0   0.0
5      10.0   0.0   0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM