0

Hello I have the following code, in order to do a matrix of adjacency, but I have the weight by peer in another column, how can do the adjacency matrix in panda with this data?

Current code:

ady = pd.read_csv("edges.csv", sep=',')[['Source', 'Target', 'weight']]
ady['weight'] = pd.to_numeric(ady['weight'])
ady = pd.crosstab(ady.Source, ady.Target, ady.weight, aggfunc = sum)

Data:

Source, Target, weight
a,b,2
a,c,1
b,a,2
b,b,1
c,a,1

Expected data:

  a,b,c
a 0,2,1
b 2,1,0
c 1,0,0

dtypes:

ady.dtypes
Source     object
Target     object
weight    float64

Original data: https://pastebin.com/Y55a64yz

Any idea?

Thanks

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84

2 Answers2

0

You could simply pivot the adi DataFrame:

adi.pivot(*adi.columns)

gives:

Target  a  b  c
Source         
a       0  2  1
b       2  1  0
c       1  0  0
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Finnaly I needed iterate over all peers in the data and set weight to 0.

ady = pd.read_csv("edges.csv", sep=',')[['Source', 'Target', 'weight']]
for i in ady['Source'].unique():
    for j in ady['Target'].unique():
        filter_a = ady['Source']==i
        filter_b = ady['Target']==j
        if ady[filter_a & filter_b]['weight'].shape == (0,):
            ady = ady.append({'Source':i, 'Target':j, 'weight':0 }, ignore_index=True)
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84