3

Say I have the dataset from the example here:

import pandas as pd

raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'], 
        'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'], 
        'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'], 
        'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])
df

enter image description here

I want to do a boxplot of regiment vs preTestScore. For that, I need to find out the relative distribution of these 2 variables. So, I group regiment by preTestScore:

df1 = df['regiment'].groupby(df['preTestScore']).count()
df1

preTestScore
2     3
3     3
4     2
24    2
31    2
Name: regiment, dtype: int64

If I try to do the boxplot now, it gives an error:

import seaborn as sns
sns.boxplot(data=df1)

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-131-8296ca940a25> in <module>()
      1 df1 = df['regiment'].groupby(df['preTestScore']).count()
      2 df1
----> 3 sns.boxplot(data=df1)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\categorical.py in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth, whis, notch, ax, **kwargs)
   2209     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
   2210                           orient, color, palette, saturation,
-> 2211                           width, dodge, fliersize, linewidth)
   2212 
   2213     if ax is None:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\categorical.py in __init__(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth)
    439                  width, dodge, fliersize, linewidth):
    440 
--> 441         self.establish_variables(x, y, hue, data, orient, order, hue_order)
    442         self.establish_colors(color, palette, saturation)
    443 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
     94                 if hasattr(data, "shape"):
     95                     if len(data.shape) == 1:
---> 96                         if np.isscalar(data[0]):
     97                             plot_data = [data]
     98                         else:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
    765         key = com._apply_if_callable(key, self)
    766         try:
--> 767             result = self.index.get_value(self, key)
    768 
    769             if not is_scalar(result):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
   3116         try:
   3117             return self._engine.get_value(s, k,
-> 3118                                           tz=getattr(series.dtype, 'tz', None))
   3119         except KeyError as e1:
   3120             if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

So, I convert the groupby object into a dataframe and try the boxplot again:

df1 = pd.DataFrame(df1)
df1

enter image description here

sns.boxplot(data=df1)

enter image description here

This produces a boxplot, but the distribution is not that of regiment vs preTestScore (in fact, this boxplot doesn't make sense to me; I don't know what its y axis values represent). For that, we need to specify the x and y parameters in the boxplot. But, since the groupby object is not a dataframe, it produces the following error:

sns.boxplot(x='regiment', y='preTestScore', data=df1)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-132-fc8036eb7d0b> in <module>()
----> 1 sns.boxplot(x='regiment', y='preTestScore', data=df1)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\categorical.py in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth, whis, notch, ax, **kwargs)
   2209     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
   2210                           orient, color, palette, saturation,
-> 2211                           width, dodge, fliersize, linewidth)
   2212 
   2213     if ax is None:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\categorical.py in __init__(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth)
    439                  width, dodge, fliersize, linewidth):
    440 
--> 441         self.establish_variables(x, y, hue, data, orient, order, hue_order)
    442         self.establish_colors(color, palette, saturation)
    443 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    149                 if isinstance(input, string_types):
    150                     err = "Could not interpret input '{}'".format(input)
--> 151                     raise ValueError(err)
    152 
    153             # Figure out the plotting orientation

ValueError: Could not interpret input 'regiment'

We can check the datatype of df1 by doing:

df1.dtype
>>> dtype('int64')

When I take the values in df1 and put them into a new dataframe df2, and try the boxplot again, it works:

df2 = pd.DataFrame({'preTestScore': [2,3,4,24,31], 'regiment': [3,3,2,2,2]})
df2

enter image description here

sns.boxplot(x='regiment', y='preTestScore', data=df2)

enter image description here

So, instead of copying the contents of the groupby object and pasting them into a new dataframe, how can I directly get a dataframe to store the relative distribution of two variables in a dataframe?

Kristada673
  • 3,512
  • 6
  • 39
  • 93

1 Answers1

2

Use to_frame to convert the Series to a DataFrame and then reset the index before plotting:

df1 = df['regiment'].groupby(df['preTestScore']).count().to_frame().reset_index()
sns.boxplot(x='regiment', y='preTestScore', data=df1)
Shaido
  • 27,497
  • 23
  • 70
  • 73
  • Unrelated, but could you also say how to use `groupby` with a fixed number of bins? This would be helpful in situations where, say, `regiment` has >10 values and we want to group the dataframe into 3 bins `regiment` values. – Kristada673 Aug 14 '18 at 10:29
  • @Kristada673: Simply create a new column with binned values and group by that one. See for example here on how the binning can be done: https://stackoverflow.com/questions/45273731/binning-column-with-python-pandas – Shaido Aug 14 '18 at 10:34