Two approaches using NumPy tools -
Approach #1
def edgelist(df):
a = df.values
c = df.columns
n = len(c)
c_ar = np.array(c)
out = np.empty((n, n, 2), dtype=c_ar.dtype)
out[...,0] = c_ar[:,None]
out[...,1] = c_ar
mask = ~np.eye(n,dtype=bool)
df_out = pd.DataFrame(out[mask], columns=[['Source','Target']])
df_out['Weight'] = a[mask]
return df_out
Sample run -
In [155]: df
Out[155]:
A B C D
A 0.0 0.5 0.5 0.0
B 1.0 0.0 0.0 0.0
C 0.8 0.0 0.0 0.2
D 0.0 0.0 1.0 0.0
In [156]: edgelist(df)
Out[156]:
Source Target Weight
0 A B 0.5
1 A C 0.5
2 A D 0.0
3 B A 1.0
4 B C 0.0
5 B D 0.0
6 C A 0.8
7 C B 0.0
8 C D 0.2
9 D A 0.0
10 D B 0.0
11 D C 1.0
Approach #2
# https://stackoverflow.com/a/46736275/ @Divakar
def skip_diag_strided(A):
m = A.shape[0]
strided = np.lib.stride_tricks.as_strided
s0,s1 = A.strides
return strided(A.ravel()[1:], shape=(m-1,m), strides=(s0+s1,s1))
# https://stackoverflow.com/a/48234170/ @Divakar
def combinations_without_repeat(a):
n = len(a)
out = np.empty((n,n-1,2),dtype=a.dtype)
out[:,:,0] = np.broadcast_to(a[:,None], (n, n-1))
out.shape = (n-1,n,2)
out[:,:,1] = onecold(a)
out.shape = (-1,2)
return out
cols = df.columns.values.astype('S1')
df_out = pd.DataFrame(combinations_without_repeat(cols))
df_out['Weight'] = skip_diag_strided(df.values.copy()).ravel()
Runtime test
Using @cᴏʟᴅsᴘᴇᴇᴅ's timing setup
:
In [704]: x = np.random.randn(1000, 1000)
...: x[[np.arange(len(x))] * 2] = 0
...:
...: df = pd.DataFrame(x)
# @cᴏʟᴅsᴘᴇᴇᴅ's soln
In [705]: %%timeit
...: df.index.name = 'Source'
...: df.reset_index()\
...: .melt('Source', value_name='Weight', var_name='Target')\
...: .query('Source != Target')\
...: .reset_index(drop=True)
10 loops, best of 3: 67.4 ms per loop
# @Wen's soln
In [706]: %%timeit
...: df.values[[np.arange(len(df))]*2] = np.nan
...: df.stack().reset_index()
100 loops, best of 3: 19.6 ms per loop
# Proposed in this post - Approach #1
In [707]: %timeit edgelist(df)
10 loops, best of 3: 24.8 ms per loop
# Proposed in this post - Approach #2
In [708]: %%timeit
...: cols = df.columns.values.astype('S1')
...: df_out = pd.DataFrame(combinations_without_repeat(cols))
...: df_out['Weight'] = skip_diag_strided(df.values.copy()).ravel()
100 loops, best of 3: 17.4 ms per loop