【发布时间】:2018-03-22 13:10:29
【问题描述】:
我有一个如下形式的 pandas 数据框:
index | id | group
0 | abc | A
1 | abc | B
2 | abc | B
3 | abc | C
4 | def | A
5 | def | B
6 | ghi | B
7 | ghi | C
我想将其转换为加权图/邻接矩阵,其中节点是“组”,权重是每个组对的共享 ID 的总和:
权重是每个 id 的组对组合的计数,所以:
AB = 'abc' indexes (0,1),(0,2) + 'def' indexes (4,5) = 3
AC = 'abc' (0,3) = 1
BC = 'abc' (2,3), (1,3) + 'ghi' (6,7) = 3
得到的矩阵是:
A |B |C
A| 0 |3 |1
B| 3 |0 |3
C| 1 |3 |0
目前我这样做的效率很低:
f = df.groupby(['id']).agg({'group':pd.Series.nunique}) # to count groups per id
f.loc[f['group']>1] # to get a list of the ids with >1 group
# i then for loop through the id's getting the count of values per pair (takes a long time).
这是第一次通过粗略的黑客方法,我确信必须有使用 groupby 或 crosstab 的替代方法,但我无法弄清楚。
【问题讨论】:
-
正确:已编辑
标签: python pandas dataframe matrix data-structures