【问题标题】:Create strange edge matrix [duplicate]创建奇怪的边缘矩阵[重复]
【发布时间】:2020-03-21 01:42:34
【问题描述】:

我有一个像这样的数据框:

import pandas as pd

df = pd.DataFrame(columns = ['id', 'tag'])

df['id'] = (['1925782942580621034', '1925782942580621034',
   '1925782942580621034', '1925782942580621034',
   '1930659617975470678', '1930659617975470678',
   '1930659617975470678', '1930659617975470678',
   '1930659617975470678', '1930659617975470678',
   '1930659617975470678', '1930659617975470678',
   '1971229370376634911', '1971229370376634911',
   '1971229370376634911', '1971229370376634911',
   '1971229370376634911', '1971229370376634911',
   '1971229370376634911', '1971229370376634911',
   '1971229370376634911'])

df['tag'] = (['nintendo', 'cosmetic', 'pen', 'office supplies', 'holding',
   'person', 'hand', 'text', 'design', 'pen', 'office supplies',
   'cosmetic', 'tool', 'office supplies', 'weapon', 'indoor',
   'everyday carry', 'pen', 'knife', 'electronics', 'case'])

df

我想努力获得类似的东西:

df_wish = pd.DataFrame(columns = ['id_source', 'id_target', 'common_tags'])

地点:

df_with['id_source'] #is the "id" that we are taking care of
df_with['id_target'] #is the "id" that has at least one "tag" in common with "id_source"
df_with['common_tags'] #is the number of shared "tag" between "id_source" and "id_target"

你能帮帮我吗?非常感谢

【问题讨论】:

  • 你有多少个标签/ID?
  • 在我的回答之后也见 cmets。
  • 类似 15k 个唯一 ID 和大约 100k 个唯一标签。我有 32GB RAM 内存和 i7 cpu。谢谢

标签: python python-3.x pandas gephi


【解决方案1】:

如果你没有太多的标签/ID,你可以做一个crosstab并广播:

s = pd.crosstab(df['id'], df['tag'])
idx = s.index

s = s.values
pd.DataFrame(np.sum(s[None,:] & s[:, None], axis=-1), 
             index=idx, columns=idx)

输出:

                       1925782942580621034    1930659617975470678    1971229370376634911
-------------------  ---------------------  ---------------------  ---------------------
1925782942580621034                      4                      3                      2
1930659617975470678                      3                      8                      2
1971229370376634911                      2                      2                      9

【讨论】:

  • 你也可以s = pd.crosstab(df['id'], df['tag']); s @ s.T.
  • @jdehesa 这实际上是一个很好的解决方案。为什么我没有想到:-)。
  • 好吧,我刚刚回答了nearly the same question :) 关于完整的问题,我想完整请求的输出可以用df2 = (s @ s.T).unstack(); df2.name = 'common_tags'; df2.index.names = ['id_source', 'id_target']; out = df2.reset_index() 之类的东西形成
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多