使用熊猫分组数据的箱形图

时间:2017-01-05 19:39:43

标签: python pandas

我正在尝试使用python的pandas库创建一些分组数据的箱图。每个性别和pclass的输出应该是一个盒子图,总共6个。我的代码如下:

string

但我收到了一个错误:

import pandas as pd

df = pd.DataFrame({"Age":[34,12,78,84,26,30], 
                   "Sex":['Male','Female', 'Male','Female', 'Male','Female'], 
                   "Pclass":[1,1,2,2,3,3], 
                   "Survived":[0,0,0,1,1,1]})

df.groupby(['Sex','Pclass']).boxplot(column='Age', by='Survived')

这是一个错误还是我做错了什么?

我的pd.show_versions()输出:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-4241a9c73bb3> in <module>()
      6                    "Survived":[0,0,0,1,1,1]})
      7 
----> 8 df.groupby(['Sex','Pclass']).boxplot(column='Age', by='Survived')

/home/leokury/anaconda2/lib/python2.7/site-packages/pandas/tools/plotting.pyc in boxplot_frame_groupby(grouped, subplots, column, fontsize, rot, grid, ax, figsize, layout, **kwds)
   3110                               rot=rot, grid=grid, **kwds)
   3111             ax.set_title(pprint_thing(key))
-> 3112             ret.loc[key] = d
   3113         fig.subplots_adjust(bottom=0.15, top=0.9, left=0.1,
   3114                             right=0.9, wspace=0.2)

/home/leokury/anaconda2/lib/python2.7/site-packages/pandas/core/indexing.pyc in __setitem__(self, key, value)
    138         else:
    139             key = com._apply_if_callable(key, self.obj)
--> 140         indexer = self._get_setitem_indexer(key)
    141         self._setitem_with_indexer(indexer, value)
    142 

/home/leokury/anaconda2/lib/python2.7/site-packages/pandas/core/indexing.pyc in _get_setitem_indexer(self, key)
    125 
    126         try:
--> 127             return self._convert_to_indexer(key, is_setter=True)
    128         except TypeError as e:
    129 

/home/leokury/anaconda2/lib/python2.7/site-packages/pandas/core/indexing.pyc in _convert_to_indexer(self, obj, axis, is_setter)
   1228                 mask = check == -1
   1229                 if mask.any():
-> 1230                     raise KeyError('%s not in index' % objarr[mask])
   1231 
   1232                 return _values_from_object(indexer)

KeyError: "['Female' 1] not in index"

0 个答案:

没有答案