ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。df[条件]

Pre*_*amm 4 python pandas

如果“embark_town”列中的数据是“南安普敦”,我想将它们全部更改为“曼彻斯特”。因此,在通过条件设置访问数据后,我应用了“应用”功能。有什么问题?

# Import Packages
import pandas as pd 
import numpy as np
import seaborn as sns

# dataset upload
df = sns.load_dataset("titanic")
df = df.rename(columns={'pclass':'passenger_class','sex':'gender','age':'old'})

def change(name):
  if name == 'Southampton':
    name = 'Manchester'
  return name

condition = (df.embark_town == 'Southampton')

df[condition] = df[condition].apply(change)
df

Run Code Online (Sandbox Code Playgroud)

得到一个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-2cf6d75dce9e> in <module>()
     14 
     15 condition = (df.embark_town == 'Southampton')
---> 16 df[condition] = df[condition].apply(change)
     17 df
     18 # df['embark_town'] = df['embark_town'].apply(change)

5 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __nonzero__(self)
   1328     def __nonzero__(self):
   1329         raise ValueError(
-> 1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1332         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Run Code Online (Sandbox Code Playgroud)

lan*_*ane 6

正如 Michael Szczesny 在评论中也指出的那样。DataFrame.apply使用 aSeries作为输入。定义的函数 change(name)需要一个字符串。该消息ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().来自尝试将 aSeries与字符串进行比较。

\n

Register Sole 指出的一项修复方法是使用条件。

\n
condition = (df[\xe2\x80\x98embark_town\xe2\x80\x99] == 'Southampton')\ndf[condition]['embark_town'] = 'Manchester'\n
Run Code Online (Sandbox Code Playgroud)\n

要继续使用 apply,更改函数需要如下所示:

\n
def change(series):\n  if series.name == 'embark_town':\n      series[series.values == 'Southampton'] = 'Manchester'\n\n  return series\n
Run Code Online (Sandbox Code Playgroud)\n