【问题标题】:Find the categories that frequently occur together based on another column根据另一列查找经常一起出现的类别
【发布时间】:2022-01-03 15:20:34
【问题描述】:

假设我在 Pandas 数据框中有以下数据:

Paper ID Author ID
Paper_1 Author_1
Paper_1 Author_2
Paper_2 Author_2
Paper_3 Author_1
Paper_3 Author_2
Paper_3 Author_3
Paper_4 Author_1
Paper_4 Author_3

我需要找出非零协作的数量。所以,输出应该是:
(Author_1,Author_2) --> 2
(Author_1,Author_3) --> 1

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: python pandas dataframe numpy data-cleaning


    【解决方案1】:

    如果数据相当小,那么在Paper ID 上合并将生成可以折叠/聚合的对:

    # assume df has columns Paper ID, Author ID
    df_merged = df.merge(df, on="Paper ID")
    
    # keep only one instance of a collaboration
    mask = df_merged["Author ID_x"] > df_merged["Author ID_y"]
    
    # aggregate (note the use of the mask to avoid double-
    # counting and self-collaborations as noted in the
    # comment by Riccardo Bucco)
    counts = (
        df_merged[mask]
        .groupby(["Author ID_x", "Author ID_y"])
        .agg(collaboration_count=("Paper ID", "count"))
    )
    

    【讨论】:

    • 接近 35,000 行。
    • 您还返回了自我合作以及 A 和 B 以及 B 和 A 之间的合作
    • 好点,让我解决这个问题。感谢您指出这一点!
    • 摆脱无用合作的好方法,谢谢:)
    • 谢谢你。它有效。
    猜你喜欢
    • 2021-12-30
    • 2022-11-15
    • 2021-04-12
    • 1970-01-01
    • 2021-07-25
    • 2020-04-24
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多