1

I have a table and I want to convert it using Python 3x Pandas into the following:

Group Assessment Review
GroupA No team spirit Negative
GroupA Good players Positive
GroupA They scored well Positive
GroupB Goal failed Negative
GroupB Bad weather Negative
GroupB Resilience Positive
GroupB Growth potential Positive
GroupB Bad technique Negative

Resulting table should be:

Group Positive Negative
GroupA Good players No team spirit
GroupA They scored well NaN
GroupB Resilience Goal Failed
GroupB Growth Potential Bad weather
GroupB NaN Bad Technique

Is there any neat, Pythonic way of doing it using Pandas or other method?

2 Answers 2

2

You can't pivot as is, but you could add a group number using groupby + cumcount and pivot using the newly created numbering as index:

out = (df.assign(num=df.groupby(['Group','Review']).cumcount())
       .pivot(['num','Group'],'Review','Assessment')
       .droplevel(0).sort_index()
       [['Positive','Negative']]
       .reset_index()
       .rename_axis(columns=[None]))

Output:

    Group          Positive        Negative
0  GroupA      Good players  No team spirit
1  GroupA  They scored well             NaN
2  GroupB        Resilience     Goal failed
3  GroupB  Growth potential     Bad weather
4  GroupB               NaN   Bad technique
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. Can you explain what the "num=" parameters serves for in the assign method?
@matt.aurelio it's creating a new column named "num", which we use to pivot
0

it is not what you exactly want, but it seems this table structure has more sence:

res = (df.groupby(['Group','Review']).
       apply(lambda x:x['Assessment'].tolist()).
       unstack())
print(res)
'''
Review                                   Negative                          Positive
Group                                                                              
GroupA                           [No team spirit]  [Good players, They scored well]
GroupB  [Goal failed, Bad weather, Bad technique]    [Resilience, Growth potential]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.