Search code examples
pythonpivot-table

Python pivot on dataframe and return values as strings


I have a Dataframe, userID,Item and Score. I would like to get a pivot on ItemID and have top scored Items on values field. Is it possible to get Items on pivot result. Scores I can get with Max/Mean and similar methods but I couldnt figure out how to get string values.

Here is my Dataframe:

My Dataframe

This is what I'm trying to achive:

What I want to achive


Solution

  • This code will create a row for each userid and create columns for items in the order of the score

    df = pd.DataFrame({'userid': ['A', 'A', 'A', 'B', 'B'],
                       'item': ['ford', 'renault', 'fiat', 'ford', 'jaguar'],
                       'score': [1, 5, 1, 4, 2]})
    df['preference'] = df.groupby(['userid'])['score'].rank(method="first", ascending=False)
    df_pivot = df.pivot(index='userid', columns="preference", values="item")
    print(df_pivot)
    

    If there are unequal rows for the userid then a NaN will fill the gaps.

    One question is what should happen if there is a tie in the score column; in the code I wrote above I use the first row in the data frame. This means that the preference column is always 1, 2, 3, 4... within each userid group, which leads to a nicer presentation of the preference columns.