2 回答
TA贡献1862条经验 获得超7个赞
决定对这个问题找点乐子。
您可以将node1和转换node2为 Categorical dtype,然后使用groupby.
from functools import partial
vals = np.unique(df[['node1', 'node2']])
p = partial(pd.Categorical, categories=vals)
df['node1'], df['node2'] = p(df['node1']), p(df['node2'])
(df.groupby(['node1', 'node2'])
.first()
.fillna(0, downcast='infer')
.weight
.unstack())
node2 a b c d
node1
a 0 0 1 0
b 0 0 2 0
c 0 0 0 0
d 0 0 3 0
另一种选择是直接设置底层数组值。
df2 = pd.DataFrame(0, index=vals, columns=vals)
f = df2.index.get_indexer
df2.values[f(df.node1), f(df.node2)] = df.weight.values
print(df2)
a b c d
a 0 0 1 0
b 0 0 2 0
c 0 0 0 0
d 0 0 3 0
TA贡献2037条经验 获得超6个赞
使用pivot与reindex
In [20]: vals = np.unique(df[['node1', 'node2']])
In [21]: df.pivot(index='node1', columns='node2', values='weight'
).reindex(columns=vals, index=vals, fill_value=0)
Out[21]:
node2 a b c d
node1
a 0 0 1 0
b 0 0 2 0
c 0 0 0 0
d 0 0 3 0
或者使用set_index和unstack
In [27]: (df.set_index(['node1', 'node2'])['weight'].unstack()
.reindex(columns=vals, index=vals, fill_value=0))
Out[27]:
node2 a b c d
node1
a 0 0 1 0
b 0 0 2 0
c 0 0 0 0
d 0 0 3 0
添加回答
举报