3

If the data in the column 'embark_town' is 'Southampton', I want to change them all to 'manchester'. So after accessing the data with condition setting, I applied the 'apply' function. What's the problem?

# 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

Get an error:

---------------------------------------------------------------------------
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().
Preamm
  • 71
  • 1
  • 1
  • 7
  • 1
    I usually do it this way, does it help? condition = (df[‘embark_town’] == 'Southampton') – Register Sole Jan 22 '22 at 02:26
  • 1
    You apply `change` on every column (axis=0) of `df`. *name* becomes a `pd.Series` as a parameter for `change` causing the `ValueError` inside `change` . `df.embark_town[condition] = 'Manchester'` should work as well without `apply`. – Michael Szczesny Jan 22 '22 at 03:15
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – iftiben10 May 29 '22 at 11:29

2 Answers2

5

As Michael Szczesny also pointed out in the comment. DataFrame.apply uses a Series as input. The change(name) function defined expects a string. The message ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). comes from trying to compare a Series to a string.

One fix pointed out by Register Sole is to use conditions instead.

condition = (df[‘embark_town’] == 'Southampton')
df[condition]['embark_town'] = 'Manchester'

To keep using apply, the change function would need to look something like this:

def change(series):
  if series.name == 'embark_town':
      series[series.values == 'Southampton'] = 'Manchester'

  return series
lane
  • 766
  • 5
  • 20
0

In Python it [and / or ] operator require truth-values and in pandas, these are considered as ambiguous, so you have to use "bitwise" operator or --> |
and --> &