giovanni sergi
giovanni sergi

Reputation: 35

How can I apply this function in a correct way?

I’m working on a dataset I found here.

I tried to write a function to convert each value of column BOROUGH from digit to names. Like this:

# Manhattan (1), Bronx (2), Brooklyn (3), Queens (4), and Staten Island (5)

## convert BOROUGHS from int to string

df['BOROUGH'] = df['BOROUGH'].astype(str)

## create a function to replace number with name

def name_boro(s):
    if s == '1':
        return 'Manhattan'
    elif s == '2':
        return 'Bronx'
    elif s == '3':
        return 'Brooklyn'
    elif s == '4':
        return 'Queens'
    else:
        return 'Staten Island'
    
df.apply(name_boro(df['BOROUGH']))

The output message is this:

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in 19 return 'Staten Island' 20 ---> 21 df.apply(name_boro(df['BOROUGH']))

in name_boro(s) 8 9 def name_boro(s): ---> 10 if s == '1': 11 return 'Manhattan' 12 elif s == '2':

~\anaconda3\lib\site-packages\pandas\core\generic.py in nonzero(self) 1327 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()."

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

Would like to help me?

Thank you, Giovanni

Upvotes: 0

Views: 71

Answers (3)

BleakHeart
BleakHeart

Reputation: 51

If you have a large df, it is not worth using an apply method. Instead, you could use a map method as follows:

# define your dictionary
num_to_name = {'1': 'Manhattan', '2': 'Bronx', '3': 'Brooklyn', '4': 'Queens'}
# map the values in BOROUGH column
df['BOROUGH'] = df['BOROUGH'].map(num_to_name)

Upvotes: 1

PieCot
PieCot

Reputation: 3629

You can use map, but using a default value. BTW, you do not need to convert the original numbers to strings.

df.BOROUGH.map(lambda x: {1: 'Manhattan', 2: 'Bronx', 3: 'Brooklyn', 4: 'Queens'}.get(x, 'Staten Island'))

Upvotes: 0

SomeDude
SomeDude

Reputation: 14228

You can do:

df['BOROUGH'] = df['BOROUGH'].apply(name_boro)

Upvotes: 0

Related Questions