0

So let's say I have a dataframe of this structure:

╔══════════════════════════════════════╦══════════════════╦════════════╦═══════════╦════════════════════╦══════════════╦══════════════════════╦═════════════════════╦═════════════════════════════════╦════════════════════════════════════════════╦════════════════════════════════════════════╗
║             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  ║    [email protected]   ║     xxxx     ║        Google        ║          3          ║    Bachelor's / Undergraduate   ║ Indian Institute of Information Technology ║ Technical / Quantitative~>Computer Science ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 71cfc6b7-b601-4ca0-b894-0b9f48dad717 ║     kami-kms     ║    Kami    ║    Kms    ║  [email protected]  ║    xxxxxxx   ║        Groupon       ║          2          ║ High School Certificate/Diploma ║             Board of Education             ║    Technical / Quantitative~>Statistics    ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 71cfc6b7-b601-4ca0-b894-0b9f48dad717 ║     kami-kms     ║    Kami    ║    Kms    ║  [email protected]  ║    xxxxxxx   ║        Groupon       ║          2          ║    Bachelor's / Undergraduate   ║               ISBM University              ║    Technical / Quantitative~>Statistics    ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 8af6be9a-007e-4cfa-9965-aa65948e945f ║   kundan-mishra  ║   KUNDAN   ║   MISHRA  ║ [email protected] ║   xxxxxxxx   ║       Mobikwik       ║          8          ║       Master's / Graduate       ║                     IIM                    ║       Business / Commerce~>Marketing       ║
╠══════════════════════════════════════╬══════════════════╬════════════╬═══════════╬════════════════════╬══════════════╬══════════════════════╬═════════════════════╬═════════════════════════════════╬════════════════════════════════════════════╬════════════════════════════════════════════╣
║ 8af6be9a-007e-4cfa-9965-aa65948e945f ║   kundan-mishra  ║   KUNDAN   ║   MISHRA  ║ [email protected] ║    xxxxxxx   ║ OYO - Vacation Homes ║          8          ║       Master's / Graduate       ║                     IIM                    ║       Business / Commerce~>Marketing       ║
╚══════════════════════════════════════╩══════════════════╩════════════╩═══════════╩════════════════════╩══════════════╩══════════════════════╩═════════════════════╩═════════════════════════════════╩════════════════════════════════════════════╩════════════════════════════════════════════╝

I want to groupby all elements using candidate_id field. So, what I want is:

╔══════════════════════════════════════╦══════════════════╦════════════╦═══════════╦════════════════════╦══════════════╦═══════════╦══════════════════════╦═════════════════════╦═════════════════════════════════╦════════════════════════════════════════════╦════════════════════════════════════════════╦════════════════════════════╦═════════════════╦══════════════════════════════════════╗
║             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  ║    [email protected]   ║     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    ║  [email protected]  ║    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  ║ [email protected] ║   xxxxxxxx   ║  Mobikwik ║ OYO - Vacation Homes ║          8          ║       Master's / Graduate       ║                     IIM                    ║       Business / Commerce~>Marketing       ║ NA                         ║ NA              ║ NA                                   ║
╚══════════════════════════════════════╩══════════════════╩════════════╩═══════════╩════════════════════╩══════════════╩═══════════╩══════════════════════╩═════════════════════╩═════════════════════════════════╩════════════════════════════════════════════╩════════════════════════════════════════════╩════════════════════════════╩═════════════════╩══════════════════════════════════════╝

I am unable to find any way other than brute force to do this.

0

1 Answer 1

1

I think you need GroupBy.cumcount for counter, create MultiIndex by DataFrame.set_index and reshape by DataFrame.unstack, sorting MultiIndex in columns by second level and flatten columns names by 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()
2
  • Is there any way that some entries won't go at the end even though it is only second entry for the current uuid. So what is happening that after using this code I am getting a lot of empty columns in between legit entries. I want the entries to be denser.
    – Rajarshi
    Sep 17, 2020 at 9:19
  • @Rajarshi - Unfortuantely it depends of data, pandas also add NaNs for not exist rows (if there is at least one row filled by data, all another rows are filled by NaNs)
    – jezrael
    Sep 17, 2020 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.