'How do I solve the Key Error while using the pandas library?
if I use the following code I get an Error Message. Does someone know what I am doing wrong?
print(data_wald_nan)
r = [scipy.stats.pearsonr(x=data_wald_nan["vaxx_rate"], y=data_wald_nan[independend_variable])]
print()
y = data_wald_nan["vaxx_rate"]
x = data_wald_nan[independend_variable]
fig = sns.scatterplot(data=data_wald_nan, y="vaxx_rate", x=independend_variable, c = colors)
fig.set_title( "Waldorfschools per million capitas and \n vaccination rate "
"in countries \n (correlation-coefficient: " + str(r) +
")", weight="bold")
fig.set_ylabel("Vaccination Rate in %")
fig.set_xlabel("Share of Waldorfschools per million capitas")
for line in range(0, data_wald_nan.shape[0]):
fig.text(data_wald_nan.Wald_Schools_per_Mln_People[line] + 0.01, data_wald_nan.vaxx_rate[line],
data_wald_nan.Country[line], horizontalalignment='left',
size='medium', color='black', weight='semibold')
fig.set_ylim(ymin=20, ymax=100)
fig.set_xlim(xmin=0,xmax=9)
plt.grid(visible=True, color="grey", linestyle="-", linewidth=0.5, alpha=0.2)
plt.show()
This is the error message:
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0
EDIT (The full Errorcode) It says I have to add more text to this question but I dont know what to type:
Traceback (most recent call last):
File "C:\Users\david\OneDrive\Dokumente\Dokumente\uni\CSS\FCSS\data\scatter.py", line 55, in <module>
scatter("Wald_Schools_per_Mln_People")
File "C:\Users\david\OneDrive\Dokumente\Dokumente\uni\CSS\FCSS\data\scatter.py", line 38, in scatter
fig.text(data_wald_nan.Wald_Schools_per_Mln_People[line] + 0.01, data_wald_nan.vaxx_rate[line],
File "C:\Users\david\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 942, in __getitem__
return self._get_value(key)
File "C:\Users\david\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 1051, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\david\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 0
Solution 1:[1]
Change this line of code:
fig.text(data_wald_nan.Wald_Schools_per_Mln_People[line] + 0.01...
to
fig.text(data_wald_nan.Wald_Schools_per_Mln_People.iloc[line] + 0.01...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
