Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pivot a dataframe with repeating values

I have a dataframe like this:

df = pd.DataFrame({'id':{1,2,3,4,4},
'course':{'english', 'art', 'math', 'english', 'chem'},
'result':{'a','b','c','a','b'}})

I want to pivot this such that:

course  English art math sci chem 
id
1         a      -    -   -    -   
2         -      b    -   -    -
3         -      -    c   -    -
4         a      -    -   -    b

Note: There are repeated values in ID, results & Courses. Due to the duplicate values, I am not able to pivot it.


1 Answers

pivot works just fine. I think the issue is with your input data to dataframe constructor. You have a set for each key, so the duplicates disappear. Use a list and it works.

# use a list for values
df = pd.DataFrame({'id':[1,2,3,4,4],
                   'course':['english', 'art', 'math', 'english', 'chem'],
                   'result':['a','b','c','a','b']})
# pivot and reindex
new_df = (
    df.pivot(index='id', columns='course', values='result')
    .reindex(columns=['english', 'art', 'math', 'sci', 'chem'])
    .fillna('-')
)

enter image description here

like image 176
cottontail Avatar answered Jan 01 '26 10:01

cottontail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!