首页 文章

Pandas中的ValueError:系列的真值是不明确的 . 使用a.empty,a.bool(),a.item(),a.any()或a.all()

提问于
浏览
-1

我试图替换我的数据帧的 Headers 列中的值 . 然而 . 它的投掷错误

for i in range(0, 1309):
    if((x['Title'][i])=='Mr'):
        x['Title'][i] = '0'
    if(x['Title'][i]=='Miss'):
        x['Title'][i] = '1'
    if(x['Title'][i]=='Mrs'):
        x['Title'][i] = '2'
    if(x['Title'][i]=='Master'):
        x['Title'][i] = '3'
    else:
        x['Title'][i] = '4'

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

2 回答

  • 0

    试试这个创建一个名字替换字典,如下所示:

    names ={'Mr':0, 'Miss':1, 'Master':2, 'Mrs':3}
    

    并使用replace重命名这些值:

    df['Title'] = df.Title.replace(names).replace({'': 4})
    

    更新:解决方案2你 Cloud 使用应用功能

    def change_names(x):
        if x == 'Mr':
            return 0
        elif x =='Miss':
            return 1
        elif x == 'Master':
            return 2
        elif x =='Mrs':
            return 3
        else:
            return 4
    
    df['Title'] = df['Title'].apply(change_names)
    
  • 1

    试试这个

    df.rename(columns = {'Mr':'0'}, inplace = True)
    

    并添加其他列

相关问题