如何解决使用if else
我尝试用seaborn为python中的dates列创建一个图形。我收到错误提示。你有解决的办法吗?
----> 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在非零中(自己)
1477
1478模棱两可,是指它与输入的两个水平都匹配
-> 1479 axis
和另一个轴的标签。
1480
1481参数
ValueError:系列的真值不明确。使用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 and 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
使其简洁明了,请参见此处的使用方法:
https://stackoverflow.com/a/60244752/6660373
,也可以使用==
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"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。