Reputation: 81
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().
Upvotes: 4
Views: 72880
Reputation: 161
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 --> &
Upvotes: 2
Reputation: 886
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
Upvotes: 6