绘图时新数据框的Keyerror
Keyerror from new dataframe when plotting
Keyerror:创建新数据帧然后尝试绘制新数据帧后出现 0。
最初,代码有助于绘制原始数据帧。
删除了少量行(~5 行)并创建了一个新的数据框。
新数据框显示没有问题,但是在尝试绘制新数据框时显示 Keyerror:0。
我试图解决这个问题但没有成功。
以下是替换、删除缺失数据和创建新数据框的脚本。
df_pre_orderset2_t = df_pre_orderset2.replace(0, np.nan)
df_pre_orderset2_top = df_pre_orderset2_t.dropna()
pd.set_option('display.max_colwidth', None)
df_pre_orderset2_to_10 = df_pre_orderset2_top.head(10)
df_pre_orderset2_top10 = pd.DataFrame(df_pre_orderset2_to_10)
df_pre_orderset2_top10
配剧情脚本如下
plt.figure(figsize=(9,7))
ax = plt.gca()
x = df_pre_orderset2_top10['warning_status']
y = df_pre_orderset2_top10['count']
n = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
t = np.arange(10)
plt.title('Warning distribution versus order sets')
plt.ylabel('Warning count by order sets')
plt.xlabel('Warning alerts')
plt.scatter(x, y, c=t, s=100, alpha=1.0, marker='^')
plt.gcf().set_size_inches(13,8)
#scatter labels
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
plt.show()
此 returns 拟议情节的完整轮廓和关键错误:0,如下所示。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
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 in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-161-0cc4009cf4a7> in <module>
16 #scatter labels
17 for i, txt in enumerate(n):
---> 18 ax.annotate(txt, (x[i],y[i]))
19
20 plt.show()
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
851
852 elif key_is_scalar:
--> 853 return self._get_value(key)
854
855 if is_hashable(key):
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
959
960 # Similar to Index.get_value, but we do not fall back to positional
--> 961 loc = self.index.get_loc(label)
962 return self.index._get_values_for_loc(self, loc, label)
963
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 0
失败的行是 ax.annotate(txt, (x[i],y[i]))
,并且在 i=0
时失败。 x
和 y
都是 Series 对象,它们是从 df_pre_orderset2_top10
中获取的列,所以我猜测当您从该数据框中删除行时,索引为 0 的行已被删除。您应该能够通过显示数据框来验证这一点。
如果是这种情况,您可以在提取 x
和 y
列之前 reset the index 到该数据框。设置 drop=True
以确保旧索引不会作为新列添加到数据框中。
df_pre_orderset2_top10.reset_index(drop=True, inplace=True)
这应该可以解决问题。
Keyerror:创建新数据帧然后尝试绘制新数据帧后出现 0。
最初,代码有助于绘制原始数据帧。 删除了少量行(~5 行)并创建了一个新的数据框。 新数据框显示没有问题,但是在尝试绘制新数据框时显示 Keyerror:0。 我试图解决这个问题但没有成功。
以下是替换、删除缺失数据和创建新数据框的脚本。
df_pre_orderset2_t = df_pre_orderset2.replace(0, np.nan)
df_pre_orderset2_top = df_pre_orderset2_t.dropna()
pd.set_option('display.max_colwidth', None)
df_pre_orderset2_to_10 = df_pre_orderset2_top.head(10)
df_pre_orderset2_top10 = pd.DataFrame(df_pre_orderset2_to_10)
df_pre_orderset2_top10
配剧情脚本如下
plt.figure(figsize=(9,7))
ax = plt.gca()
x = df_pre_orderset2_top10['warning_status']
y = df_pre_orderset2_top10['count']
n = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
t = np.arange(10)
plt.title('Warning distribution versus order sets')
plt.ylabel('Warning count by order sets')
plt.xlabel('Warning alerts')
plt.scatter(x, y, c=t, s=100, alpha=1.0, marker='^')
plt.gcf().set_size_inches(13,8)
#scatter labels
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
plt.show()
此 returns 拟议情节的完整轮廓和关键错误:0,如下所示。
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
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 in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-161-0cc4009cf4a7> in <module>
16 #scatter labels
17 for i, txt in enumerate(n):
---> 18 ax.annotate(txt, (x[i],y[i]))
19
20 plt.show()
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in __getitem__(self, key)
851
852 elif key_is_scalar:
--> 853 return self._get_value(key)
854
855 if is_hashable(key):
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
959
960 # Similar to Index.get_value, but we do not fall back to positional
--> 961 loc = self.index.get_loc(label)
962 return self.index._get_values_for_loc(self, loc, label)
963
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 0
失败的行是 ax.annotate(txt, (x[i],y[i]))
,并且在 i=0
时失败。 x
和 y
都是 Series 对象,它们是从 df_pre_orderset2_top10
中获取的列,所以我猜测当您从该数据框中删除行时,索引为 0 的行已被删除。您应该能够通过显示数据框来验证这一点。
如果是这种情况,您可以在提取 x
和 y
列之前 reset the index 到该数据框。设置 drop=True
以确保旧索引不会作为新列添加到数据框中。
df_pre_orderset2_top10.reset_index(drop=True, inplace=True)
这应该可以解决问题。