【问题标题】:Find most popular combinations查找最受欢迎的组合
【发布时间】:2019-04-12 14:12:53
【问题描述】:

我有这个数据框,我想获得文档数量最多的组合。

df = pd.read_csv('https://s3.amazonaws.com/todel162/final.csv', header=None)
df.columns = ["topic_lda", "topic_nmf", "document_count"]
df.sort_values("document_count", ascending=False)

我希望得到这样的结果...

Expected output:
    1 0 4761
    4 1 2715
    3 4 2352
    0 3 1004
    2 2 770

组合 4-0、0-0、3-0 被省略,因为 0 已经被取了

组合 1-2 被省略,因为 1 已经被取了。

其余的记录不需要,因为有 0 到 4 个主题。

【问题讨论】:

  • 你能再解释一下这个逻辑吗? combinations 4-0, 0-0, 3-0 are omitted because 0 is already taken, combination 1-2 is omitted because 1 is already taken.
  • 第一行是 1-0-4761,这意味着 lda ID 1 链接到 nmf ID 0,因此我不需要这 4 条记录。

标签: pandas


【解决方案1】:

用途:

#first filter by topic_nmf and then by topic_lda columns
df1 = df.drop_duplicates('topic_nmf').drop_duplicates(['topic_lda'])
print (df1)
    topic_lda  topic_nmf  document_count
5           1          0            4761
21          4          1            2715
19          3          4            2352
3           0          3            1004

#filter not matched rows by topic_lda and remove duplicates
df2 = df[~(df['topic_lda'].isin(df1['topic_lda']))].drop_duplicates(['topic_lda'])
print (df2)
    topic_lda  topic_nmf  document_count
12          2          2             770

#join together
df3 = pd.concat([df1, df2])
print (df3)
    topic_lda  topic_nmf  document_count
5           1          0            4761
21          4          1            2715
19          3          4            2352
3           0          3            1004
12          2          2             770

【讨论】:

  • 您的输出中有 0-0-1986,但它应该是 0-3-1004,因为 nmf 0 已经链接到第一行的 lda 1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 2022-11-12
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多