Problem with applying a function

Hi every one, 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’

When I try to apply the function:
df.apply(name_boro(df[‘BOROUGH’]))

It returns me 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 you help me?

Thank you really much!
Giovanni

Hi @giovanni.srg
I think you should use lambda function to apply your function, I tested this code after adding the the lambda function and it work

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

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['BOROUGH'] = df['BOROUGH'].apply(lambda x: name_boro(x))

An other solution is to use the map() function

df['BOROUGH'] = df['BOROUGH'].map({1: 'Manhattan', 2: 'Bronx', 3:'Brooklyn',4:'Queens', 5:'Staten Island'})
1 Like

Ok, now it compiles. But it returns only Staten Island. Why?

Can you share your code?
This is the script I used in second solution (I didn’t change the type of the BOROUGH column)

import pandas as pd

df = pd.read_csv('nyc-rolling-sales.csv', index_col=0)
df['BOROUGH'] = df['BOROUGH'].map({1: 'Manhattan', 2: 'Bronx', 3:'Brooklyn',4:'Queens', 5:'Staten Island'})
num_to_name = {1:'Manhattan', 2:'Bronx', 3:'Brooklyn', 4:'Queens', 5:'Staten Island'}
ny_sales['BOROUGH']  =  ny_sales['BOROUGH'].map(num_to_name)

It compiles but returns only NaN values

Can you try this code?

num_to_name = {'1':'Manhattan', '2':'Bronx', '3':'Brooklyn', '4':'Queens', '5':'Staten Island'}
ny_sales['BOROUGH']  =  ny_sales['BOROUGH'].map(num_to_name)

Now it worked using the same code I posted before.

I don’t understand really why sometimes Jupiter just goes crazy.

Thanks however

1 Like