0

I have the following dataframe:

Position Played Champion Result ID Side
top riven 0 1 blue
jg jax 0 1 blue
top fiora 1 1 red
jg lee sin 1 1 red
top mundo 0 2 blue
jg jax 0 2 blue
top kayle 1 2 red
jg olaf 1 2 red

and I would like to get the following dataframe:

top jg Result ID Side
riven jax 0 1 blue
fiora lee sin 1 1 red
mundo jax 0 2 blue
kayle olaf 1 2 red

Edit1: The quoted functions use aggregation (like sum, mean) and I need it to actually keep the information intact

Edit2: I tried using the following command: df.pivot(['Result', 'ID', 'Side'], 'Position Played', 'Champion').reset_index()

But I got the error: ValueError: Index contains duplicate entries, cannot reshape

Yumi
  • 1
  • 1
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 13 '22 at 20:28
  • "*I've tried using several commands*" -> you should provide them and better describe the logic – mozway Aug 13 '22 at 20:33
  • Looks like `df.pivot(['Result', 'ID', 'Side'], 'Position Played', 'Champion').reset_index()` should do the trick. – mozway Aug 13 '22 at 20:37
  • If you have duplicates you cannot `pivot` you must `pivot_table` (be careful the order of the parameters is different) and choose and aggregation method. Or deduplicate the entries with an extra index, a solution for this is clearly described in the linked duplicate. – mozway Aug 13 '22 at 20:58
  • I tried with pivot table but the format goes wrong with nan. I used the command pd.pivot_table(df,index=["ID"],values=["Result"],columns=["Position Played", "Champion"]) – Yumi Aug 13 '22 at 21:44
  • 1
    This should rather be `df.pivot_table(index=['Result', 'ID', 'Side'], columns='Position Played', values='Champion', aggfunc='first').reset_index()`, but since you don't want aggregation rather use `pivot` with deduplication. Please update your example with a duplicate to see how you want to handle it. – mozway Aug 14 '22 at 03:25

0 Answers0