【发布时间】:2020-12-27 08:01:42
【问题描述】:
我有一个试图在条形图中绘制的数据框,但我遇到了一个奇怪的错误。
import pandas
import matplotlib.pyplot as plot
.... a bunch of code that combines two different data frames to get one data frame
df = df.groupby(['title']).sum()
df.reindex()
print(df)
df.plot('bar', df['title'], df['number'])
打印语句给出:
Action 1.159667e+10
Adventure 7.086050e+09
Animation 1.159219e+10
Comedy 2.071842e+10
Crime 3.525629e+09
Drama 8.479182e+09
Family 3.705357e+09
Fantasy 3.613503e+10
History 1.261357e+09
Horror 1.034400e+09
Music 1.963180e+09
Romance 1.273498e+10
Sci-Fi 2.586427e+10
Sport 6.863091e+08
Thriller 2.245254e+10
War 1.699709e+09
然后剧情代码:df.plot('bar', df['title'], df['number'])
给出以下错误:
----------------------------------- ---------------------------- KeyError Traceback(最近一次调用 最后的) ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py 在 get_loc(self, key, method, tolerance) 2645 中尝试: -> 2646 返回 self._engine.get_loc(key) 2647 除了 KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Main_Genre'
在处理上述异常的过程中,又发生了一个异常:
KeyError Traceback(最近调用 最后)在 30 打印(df) 31 ---> 32 df.plot('bar', df['Main_Genre'], df['worldwide_gross'])
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py 在 getitem(self, key) 2798 if self.columns.nlevels > 1: 2799 return self._getitem_multilevel(key) -> 2800 indexer = self.columns.get_loc(key) 2801 if is_integer(indexer): 2802 indexer = [indexer]
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py 在 get_loc(self、key、method、tolerance) 2646
返回 self._engine.get_loc(key) 2647 除了 KeyError: -> 2648 返回 self._engine.get_loc(self._maybe_cast_indexer(key)) 2649
indexer = self.get_indexer([key],method=method,tolerance=tolerance) 2650 如果 indexer.ndim > 1 或 indexer.size > 1:pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()
我做错了什么?任何帮助将不胜感激。
谢谢
【问题讨论】:
-
试试:
df.plot.bar('title', 'number')。 -
发生同样的错误。
标签: python pandas matplotlib plot