luisfer luisfer -4 years ago 104
Python Question

Pandas: How to do a boxplot bases in rows values instead of column values?

I have some score data from a game I'm playing with friends, it looks like:

df = pd.DataFrame({'Player' : ['A', 'B', 'C', 'D', 'E'],
'Score1' : [100, 150, 110, 180, 125],
'Score2' : [200, 210, np.nan, 125, 293],
'Score3' : [50, 35, 200, 100, 180]})


If I do a
df.boxplot()
I'll get a boxplot based on the Score#, that is, based in the scores of the whole community:

enter image description here

Now I want to do a boxplot() for each player, so we can see how they rank against each other. Something like this:

enter image description here

First thing I tried was to do a boxplot of the traspose matrix:

df.T.boxplot()


But I get an error
IndexError: list index out of range


I think it has to do with the indexes created in the traspose, so I have been playing with them, but I really don't know what else to do.

Answer Source

you need to set the index as player

import pandas as pd
import numpy as np

df = pd.DataFrame({'Player' : ['A', 'B', 'C', 'D', 'E'], 
                   'Score1' : [100, 150, 110, 180, 125], 
                   'Score2' : [200, 210, np.nan, 125, 293],
                   'Score3' : [50, 35, 200, 100, 180]})
df = df.set_index('Player')
print df
df.T.boxplot()

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download