這個問題在這里已經有了答案: 系列的真值不明確。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() (11 個回答) Python-pandas:一個系列的真值是不明確的 (2 個回答) 昨天關閉。
我已經堅持了很長一段時間,但我不知道如何解決它 - 這是代碼和隨之而來的錯誤:
data = data.sort_values(by=['Happiness Rank'])
plt.figure(figsize=(20,13))
sns.scatterplot(data=data, x='Happiness Score',y='Economy (GDP per Capita)', hue = 'Year')
plt.title('Life Satisfication vs GDP per Capita',fontsize=20)
plt.xlabel('Life Satisfication',fontsize=16)
plt.ylabel('GDP per Capita',fontsize=16)
for i in range(len(data)):
plt.text(s=data.loc[i,'Country'], x=data.loc[i,'Happiness Score'] 0.01,
y=data.loc[i,'Economy (GDP per Capita)'] 0.01, fontsize=10)
plt.show()
此代碼列印出帶點的散點圖,但點旁邊沒有國家/地區名稱,這就是錯誤所在:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-c451b5b6b763> in <module>
5 plt.ylabel('GDP per Capita',fontsize=16)
6 for i in range(len(data)):
----> 7 plt.text(s=(data.loc[i,'Country']), x=(data.loc[i,'Happiness Score'] 0.01),
8 y=(data.loc[i,'Economy (GDP per Capita)'] 0.01), fontsize=10)
9 plt.show()
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/pyplot.py in text(x, y, s, fontdict, **kwargs)
3007 @_copy_docstring_and_deprecators(Axes.text)
3008 def text(x, y, s, fontdict=None, **kwargs):
-> 3009 return gca().text(x, y, s, fontdict=fontdict, **kwargs)
3010
3011
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_axes.py in text(self, x, y, s, fontdict, **kwargs)
763 **kwargs,
764 }
--> 765 t = mtext.Text(x, y, text=s, **effective_kwargs)
766 t.set_clip_path(self.patch)
767 self._add_text(t)
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/text.py in __init__(self, x, y, text, color, verticalalignment, horizontalalignment, multialignment, fontproperties, rotation, linespacing, rotation_mode, usetex, wrap, **kwargs)
149 self._x, self._y = x, y
150 self._text = ''
--> 151 self.set_text(text)
152 self.set_color(color if color is not None else rcParams["text.color"])
153 self.set_fontproperties(fontproperties)
~/opt/anaconda3/lib/python3.8/site-packages/matplotlib/text.py in set_text(self, s)
1163 if s is None:
1164 s = ''
-> 1165 if s != self._text:
1166 self._text = str(s)
1167 self.stale = True
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py in __nonzero__(self)
1440 @final
1441 def __nonzero__(self):
-> 1442 raise ValueError(
1443 f"The truth value of a {type(self).__name__} is ambiguous. "
1444 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果有人知道如何解決這個問題/可以解釋為什么會發生這種情況,我真的很感激,這樣我就可以在將來避免這樣的事情。提前致謝!
uj5u.com熱心網友回復:
您的代碼看起來像您期望的索引具有從0開始的連續值。
但很可能您的 DataFrame 具有重復值的索引。在這種情況下,loc[i,...]檢索索引為i 的所有行, 并從中回傳特定列。此類回傳值的型別是系列而不是“單個”值。
規避此問題的一種方法是遍歷行,例如使用iterrows。然后您實際上可以忽略索引值并僅從當前行中檢索感興趣的列。
因此,將您的回圈更改為:
for _, row in data.iterrows():
plt.text(s=row['Country'], x=row['Happiness Score'] 0.01,
y=row['Economy (GDP per Capita)'] 0.01, fontsize=10)
需要上面代碼中的“_”作為占位符來捕獲當前行的索引,而行實際上是一個保存當前行的系列。
然后row[…]從當前行檢索特定列的內容。
uj5u.com熱心網友回復:
你在“loc”的麻煩。你用回圈來做它,但在這種情況下你不能這樣做。谷歌“陰謀”。在 Plotly 中制作繪圖更容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374558.html
標籤:Python 熊猫 matplotlib 海生
上一篇:在顏色條中劃分不同的顏色
