Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 749
luisfer Avatar asked Mar 24 '17 02:03

luisfer


People also ask

How do I make a horizontal box plot in Python?

Horizontal Box plots We can turn the boxplot into a horizontal boxplot by two methods first, we need to switch x and y attributes and pass it to the boxplot( ) method, and the other is to use the orient=”h” option and pass it to the boxplot() method.

How do you adjust a boxplot in Python?

Steps. Set the figure size and adjust the padding between and around the subplots. Make a Pandas dataframe, i.e., two-dimensional, size-mutable, potentially heterogeneous tabular data. Make a box and whisker plot, using boxplot() method with width tuple to adjust the box in boxplot.

How do you make a boxplot for each feature in the dataset Python?

Creating Box PlotThe matplotlib. pyplot module of matplotlib library provides boxplot() function with the help of which we can create box plots. The data values given to the ax. boxplot() method can be a Numpy array or Python list or Tuple of arrays.


1 Answers

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

like image 80
plasmon360 Avatar answered Nov 14 '22 21:11

plasmon360