1
我有這樣一個DF:錯誤詮釋散點圖
Allotment C1 C2
Annex 1.0 2.0
Arnstson 1.6 1.4
Berg 2.1 4.5
Bjugstad 6.7 6.9
,我做的C1
和C2
散點圖,同時與相關Allotment
標籤。我這樣做的:
a=df.C1
b=df.C2
n=df.Allotment
with PdfPages(r'C:\plot.pdf') as pdf:
plt.title('PC1 vs. PC2 Scatterplot')
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.scatter(a,b, facecolors='none', s=20, edgecolors='b')
# use this portion to annotate each point
for i, txt in enumerate(n):
plt.annotate(txt, (a[i],b[i]), fontsize=2.5)
fig=plt.gcf()
pdf.savefig(fig)
plt.show()
但是當我執行這一行刪除Allotments
:
df=df[~df['Allotments'] .isin (['Berg', 'Annex'])]
並運行相同的代碼,我收到以下錯誤:
Traceback (most recent call last):
File "<ipython-input-58-c5ce20451164>", line 1, in <module>
runfile('H:/python codes/PC_scatterplots.py', wdir='H:/python codes')
File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "H:/python codes/PC_scatterplots.py", line 64, in <module>
plt.annotate(txt, (a[i],b[i]), fontsize=2.5)
File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\series.py", line 521, in __getitem__
result = self.index.get_value(self, key)
File "C:\Users\spotter\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\index.py", line 1595, in get_value
return self._engine.get_value(s, k)
File "pandas\index.pyx", line 100, in pandas.index.IndexEngine.get_value (pandas\index.c:3113)
File "pandas\index.pyx", line 108, in pandas.index.IndexEngine.get_value (pandas\index.c:2844)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)
File "pandas\hashtable.pyx", line 375, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7224)
File "pandas\hashtable.pyx", line 381, in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7162)
KeyError: 9L
所以解決方法是使用'iloc'代替? – Jezzamon
另請參閱Jezzamon,您怎麼看? – Kevin