创建数据透视表时出错
我正在尝试使用Pandas从数据框创建数据透视表。下面给出的是我的数据框的视图
Image Class Score
0 cybage_ad_image2.jpg palm tree 0.656
1 cybage_ad_image2.jpg building 0.611
2 cybage_ad_image2.jpg Christmas tree 0.730
3 cybage_ad_image3.jpg person 0.746
4 cybage_ad_image3.jpg person 0.633
创建数据透视表的代码
df.pivot_table(index='Image',columns='Class',values='Score', aggfunc=sum)
错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-466-b38c8e51fd79> in <module>
1 # df.set_index('Class')
----> 2 df.pivot_table(index='Image',columns='Class',values='Class',
aggfunc=sum)
3 # df.columns
c:\users\anirudhd\appdata\local\programs\python\python35\lib\site-
packages\pandas\core\frame.py in pivot_table(self, values, index, columns,
aggfunc, fill_value, margins, dropna, margins_name)
5301 aggfunc=aggfunc, fill_value=fill_value,
5302 margins=margins, dropna=dropna,
-> 5303 margins_name=margins_name)
5304
5305 def stack(self, level=-1, dropna=True):
c:\users\anirudhd\appdata\local\programs\python\python35\lib\site-
packages\pandas\core\reshape\pivot.py in pivot_table(data, values, index,
columns, aggfunc, fill_value, margins, dropna, margins_name)
84 # group by the cartesian product of the grouper
85 # if we have a categorical
---> 86 grouped = data.groupby(keys, observed=False)
87 agged = grouped.agg(aggfunc)
88 if dropna and isinstance(agged, ABCDataFrame) and
len(agged.columns):
c:\users\anirudhd\appdata\local\programs\python\python35\lib\site-
packages\pandas\core\groupby\groupby.py in _get_grouper(obj, key, axis,
level, sort, observed, mutated, validate)
3289 in_axis, name, level, gpr = False, None, gpr, None
3290 else:
-> 3291 raise KeyError(gpr)
3292 elif isinstance(gpr, Grouper) and gpr.key is not None:
3293 # Add key to exclusions
KeyError: 'Class'
任何人都可以引导我解决这个问题。谢谢