Dataframe ValueError:序列的真值不明确.使用a.empty、a.bool()、a.item()、.a



当我试图将列添加到数据帧时遇到问题,但我得到了一个错误。我的功能出了什么问题?注:PAVARDE在立陶宛语中的意思是姓。数据框架是关于总统选举的:https://www.vrk.lt/en/2019-prezidento/rezultatai(第二次投票的选举结果(csv((。功能:

def status(person):
PAVARDE = person
if PAVARDE == 'NAUSĖDA':
return 'President'
else:
return 'looser'
president_df['winner'] = president_df[['PAVARDE']].apply(status,axis=1)

我犯了一个错误:ValueError:级数的真值是不明确的。使用a.empty、a.bool((、a.item((、.any((或.all((

怎么了?你能帮我吗?

将整列传递给函数,但需要逐值传递。一开始你需要去掉多余的方括号,然后当你使用pd.Series时,你就不需要axis参数了。最后,你的代码应该是这样的:

def status(person):
PAVARDE = person
if PAVARDE == 'NAUSĖDA':
return 'President'
else:
return 'looser'
president_df['winner'] = president_df['PAVARDE'].apply(status)

或者像这样:

df['winner'] = df['PAVARDE'].apply(lambda x: 'President' if x == 'NAUSĖDA' else 'looser')

相关内容

最新更新