【问题标题】:Group by UUID with creating multiple columns for multiple entries of same UUID按 UUID 分组,为相同 UUID 的多个条目创建多个列
【发布时间】:2021-01-04 02:22:00
【问题描述】:

假设我有一个这种结构的数据框:

╔══════════════════════════════════════╦══════════════════╦════════════╦═══════════╦════════════════════╦══════════════╦══════════════════════╦═════════════════════╦═════════════════════════════════╦════════════════════════════════════════════╦════════════════════════════════════════════╗
║             candidate_id             ║     public_id    ║ first_name ║ last_name ║        email       ║ phone_number ║     company_name     ║ years_of_experience ║        normalized_degree        ║                college_name                ║             specialization_path            ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ a132a4f9-fa42-4802-8958-228952240d47 ║ divyanshu-thakur ║  Divyanshu ║   Thakur  ║    xxx@gmail.com   ║     xxxx     ║        Google        ║          3          ║    Bachelor's / Undergraduate   ║ Indian Institute of Information Technology ║ Technical / Quantitative~>Computer Science ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 71cfc6b7-b601-4ca0-b894-0b9f48dad717 ║     kami-kms     ║    Kami    ║    Kms    ║  xxxxxx@yahoo.com  ║    xxxxxxx   ║        Groupon       ║          2          ║ High School Certificate/Diploma ║             Board of Education             ║    Technical / Quantitative~>Statistics    ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 71cfc6b7-b601-4ca0-b894-0b9f48dad717 ║     kami-kms     ║    Kami    ║    Kms    ║  xxxxxx@yahoo.com  ║    xxxxxxx   ║        Groupon       ║          2          ║    Bachelor's / Undergraduate   ║               ISBM University              ║    Technical / Quantitative~>Statistics    ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 8af6be9a-007e-4cfa-9965-aa65948e945f ║   kundan-mishra  ║   KUNDAN   ║   MISHRA  ║ xxxxxxxx@gmail.com ║   xxxxxxxx   ║       Mobikwik       ║          8          ║       Master's / Graduate       ║                     IIM                    ║       Business / Commerce~>Marketing       ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 8af6be9a-007e-4cfa-9965-aa65948e945f ║   kundan-mishra  ║   KUNDAN   ║   MISHRA  ║ xxxxxxxx@gmail.com ║    xxxxxxx   ║ OYO - Vacation Homes ║          8          ║       Master's / Graduate       ║                     IIM                    ║       Business / Commerce~>Marketing       ║
╚══════════════════════════════════════╩══════════════════╩════════════╩═══════════╩════════════════════╩══════════════╩══════════════════════╩═════════════════════╩═════════════════════════════════╩════════════════════════════════════════════╩════════════════════════════════════════════╝

我想使用candidate_id 字段对所有元素进行分组。 所以,我想要的是:

╔══════════════════════════════════════╦══════════════════╦════════════╦═══════════╦════════════════════╦══════════════╦═══════════╦══════════════════════╦═════════════════════╦═════════════════════════════════╦════════════════════════════════════════════╦════════════════════════════════════════════╦════════════════════════════╦═════════════════╦══════════════════════════════════════╗
║             candidate_id             ║     public_id    ║ first_name ║ last_name ║        email       ║ phone_number ║ company_1 ║ company_2            ║ years_of_experience ║        normalized_degree1       ║                college_name1               ║            specialization_path1            ║ normalized_degree2         ║ college_name2   ║ specialization_path2                 ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬═══════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════╬═════════════════╬══════════════════════════════════════╣
║ a132a4f9-fa42-4802-8958-228952240d47 ║ divyanshu-thakur ║  Divyanshu ║   Thakur  ║    xxx@gmail.com   ║     xxxx     ║   Google  ║ NA                   ║          3          ║    Bachelor's / Undergraduate   ║ Indian Institute of Information Technology ║ Technical / Quantitative~>Computer Science ║ NA                         ║ NA              ║ NA                                   ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬═══════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════╬═════════════════╬══════════════════════════════════════╣
║ 71cfc6b7-b601-4ca0-b894-0b9f48dad717 ║     kami-kms     ║    Kami    ║    Kms    ║  xxxxxx@yahoo.com  ║    xxxxxxx   ║  Groupon  ║ NA                   ║          2          ║ High School Certificate/Diploma ║             Board of Education             ║    Technical / Quantitative~>Statistics    ║ Bachelor's / Undergraduate ║ ISBM University ║ Technical / Quantitative~>Statistics ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬═══════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════╬═════════════════╬══════════════════════════════════════╣
║ 8af6be9a-007e-4cfa-9965-aa65948e945f ║   kundan-mishra  ║   KUNDAN   ║   MISHRA  ║ xxxxxxxx@gmail.com ║   xxxxxxxx   ║  Mobikwik ║ OYO - Vacation Homes ║          8          ║       Master's / Graduate       ║                     IIM                    ║       Business / Commerce~>Marketing       ║ NA                         ║ NA              ║ NA                                   ║
╚══════════════════════════════════════╩══════════════════╩════════════╩═══════════╩════════════════════╩══════════════╩═══════════╩══════════════════════╩═════════════════════╩═════════════════════════════════╩════════════════════════════════════════════╩════════════════════════════════════════════╩════════════════════════════╩═════════════════╩══════════════════════════════════════╝

除了蛮力之外,我找不到任何其他方法。

【问题讨论】:

    标签: python-3.x pandas dataframe group-by


    【解决方案1】:

    我认为您需要GroupBy.cumcount 作为计数器,通过DataFrame.set_index 创建MultiIndex 并通过DataFrame.unstack 重塑,按二级排序MultiIndex in columns 并通过f-strings 展平列名称:

    cols = ['candidate_id',' public_id','first_name','last_name',
            'email','phone_number','years_of_experience']
    
    g = df.groupby(cols).cumcount().add(1)
    df1 = df.set_index(cols + [g]).unstack().sort_index(axis=1, level=1)
    df1.columns = df1.columns.map(lambda x: f'{x[0]}{x[1]}')
    df1 = df1.reset_index()
    

    【讨论】:

    • 有没有什么办法让一些条目不会在最后出现,即使它只是当前 uuid 的第二个条目。那么发生了什么,在使用此代码后,我在合法条目之间得到了很多空列。我希望条目更密集。
    • @Rajarshi - 不幸的是,它取决于数据,pandas 还为不存在的行添加 NaN(如果至少有一行被数据填充,则所有其他行都由 NaN 填充)
    猜你喜欢
    • 1970-01-01
    • 2015-03-05
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多