0

I am trying to convert a set of numeric identifier values for a column in a dataframe into a string value.

new_housing.replace({'borough': {'1':'Manhattan', '2':'Bronx', '3':'Brooklyn', '4':'Queens', '5':'Staten Island'}})

I have tried using the .replace function but have been getting an error.

TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'

I am fairly new to Python coding and it seems like this may be an issue related to the datatype in the column. Any help would be appreciated. Thanks

Ron Thomas
  • 737
  • 1
  • 8
  • 20
eric8395
  • 1
  • 1
  • Do you have a reproducible example? – mark_1985 Nov 29 '21 at 03:17
  • Does this answer your question? [pandas create new column based on values from other columns / apply a function of multiple columns, row-wise](https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns-apply-a-function-o) – Owenn Nov 29 '21 at 03:21
  • Using the answer above, you could override the column you want to replace instead of creating a new column. – Owenn Nov 29 '21 at 03:23

1 Answers1

0

You can do:

boroughs_dict = {'1':'Manhattan', '2':'Bronx', '3':'Brooklyn', '4':'Queens', '5':'Staten Island'}
new_housing.borough = new_housing.boroughs.map(lambda x: boroughs_dict.get(str(x),x))

This will find the number of borough converted to str and ignore values not found. If you know borough column has only valid number of boroughs (therefore no values to be ignored) you can use:

new_housing.borough = new_housing.boroughs.astype(str).map(boroughs_dict)
jwzinserl
  • 427
  • 1
  • 3
  • 7