使用 if else 在 Jupyter 中创建海运图时出错

error in creating seaborne graph in Jupyter by using if else

我尝试使用 seaborn 为 python 中的日期列创建图表。我收到以下错误消息。您知道如何解决吗?

----> 4 如果 df['MS_Date']=="s.xii" 或 df['MS_Date']=="s.xii(1)"或 df['MS_Date']=="s.xii(2)" 或 df['MS_Date']=="s.xii(in)" 或 df['MS_Date' ]==s.xii(ex)" 或 df['MS_Date']=="s.xii(med)": 5 df['MS_Date']== "12 世纪" 6 如果 df['MS_Date']=="s.xii/xiii" 或 df['MS_Date']=="s.xii/xiii":

~\anaconda\lib\site-packages\pandas\core\generic.py in nonzero(self) 1477 第1478章 -> 1479 axis 和另一个轴的标签。 1480 1481个参数

ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

代码:

if df['MS_Date']=="s.xii" or df['MS_Date']=="s.xii(1)" or df['MS_Date']=="s.xii(2)" or df['MS_Date']=="s.xii(in)" or df['MS_Date']=="s.xii(ex)" or df['MS_Date']=="s.xii(med)": 
        df['MS_Date']== "12th century"
if df['MS_Date']=="s.xii/xiii" or df['MS_Date']=="s.xii/xiii":
    df['MS_Date']=="12 & 13 century"
if df['MS_Date']=="s.xiii" or df['MS_Date']=="s.xiii(1)" or df['MS_Date']=="s.xiii(2)" or df['MS_Date']=="s.xiii(in)" or df['MS_Date']=="s.xiii(ex)" or df['MS_Date']=="s.xiii(med)":
    df['MS_Date']=="13 century"
if df['MS_Date']=="s.xiii/s.xiv" or df['MS_Date']=="s.xiii/xiv":
    df['MS_Date']=="13 & 14 century"
if df['MS_Date']=="s.xiii/s.xv":
    df['MS_Date']=="13 & 15 century"
if  df['MS_Date']=="s.xiv" or df['MS_Date']=="s.xiv(1)" or df['MS_Date']=="s.xiv(2)" or df['MS_Date']=="s.xiv(in)" or df['MS_Date']=="s.xiv(ex)"  or df['MS_Date']=="s.xiv(med)": 
    df['MS_Date']=="14th century"
if  df['MS_Date']=="s.xiv/xv":
    df['MS_Date']== "14 & 15 century"
if df['MS_Date']=="s.xv" or df['MS_Date']=="s.xv(1)" or df['MS_Date']=="s.xv(2)" or df['MS_Date']=="s.xv(in)" or df['MS_Date']=="s.xv(ex)" or df['MS_Date']=="s.xv(med)" :
    df['MS_Date']=="15th century"
if  df['MS_Date']=="s.xv/xvi":
    df['MS_Date']== "15 & 16 century"
if df['MS_Date']=="xvi" or df['MS_Date']=="s.xvi(in)":
    df['MS_Date']== "16 century"
else:
    df['MS_Date']=="unknown"



import seaborn as sns
sns.set(style="whitegrid")
fig, ax = plt.subplots()
fig.set_size_inches(25, 12)
sns.countplot(x='MS_Date', data=df, order=df['MS_Date'].value_counts().index, palette="Greens_d")
sns.set(style="white")
dfnew=nbl.describe(include=['object'])
num=dfnew.loc['count','MS_Date']
# df.iloc[0]['MS_Date']
ax.set_title('Distrubution of all manuscripts '+str(num)+' manuscripts')
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

此错误意味着您得到的是一系列布尔值,而不是单个布尔值。对于使用 if 和 alse 你需要一个单一的 True/False 值,但在这里通过使用 df['MS_Date']=="s.xii" 你会得到一个系列,它会根据它们满足的标准为你提供具有 True/False 的所有位置。

样本 df:

    Player      Position    color
0   Pele        Forward     black
1   Platini     Midfielder  white
2   Beckenbauer Defender    red

如果我想根据某些条件更改颜色列的值,那么我将这样写而不是使用 if 和 else:

df.loc[(df['Player']=='Pele') | (df['Position']=='Forward'), 'color'] = 'white'

你可以这样写 numpy.where:

np.where(<condition>, <value1 if true>, <value2 if false>) 

df['color'] = np.where((df['Player']=='Pele') | (df['Position']=='Forward'), 'black', df.color)

使用np.select使其简洁明了,请参阅此处如何使用:

你也可以使用 eq

而不是 ==

我将使用 loc 来获取我的条件成立的位置。在那个位置上,我将选择一个要更改的列。 df.loc[<conditions>, <column to assign>] = <value>

这样使用:

df.loc[(df['MS_Date']=="s.xii") | (df['MS_Date']=="s.xii(1)") | (df['MS_Date']=="s.xii(2)") | (df['MS_Date']=="s.xii(in)") | (df['MS_Date']=="s.xii(ex)") | (df['MS_Date']=="s.xii(med)"), 'MS_Date']= "12th century"
df.loc[(df['MS_Date']=="s.xii/xiii") | (df['MS_Date']=="s.xii/xiii"), 'MS_Date']="12 & 13 century"