Reputation: 23
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.
Upvotes: 0
Views: 184
Reputation: 862641
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-string
s:
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()
Upvotes: 1