1

I have a column with the following values:

City
  A
  B
  C

I want to create a heatmap but can't because this column is not an integar so I will be making it as follows:

city_new
  1
  2
  3

I have tried this case statement but it does not work

df['city_new'] = np.where(df['City']='A', 1,
                   np.where(df['City']='B', 2,
                   np.where(df['City']='C', 3)))

3 Answers3

3

You can use pandas.factorize, so that you don't have to make conditions yourself (e.g. if you have 1000 different City):

df["new_city"] = pd.factorize(df["City"])[0] + 1

Output:

  City  new_city
0    A         1
1    B         2
2    C         3
Chris
  • 29,127
  • 3
  • 28
  • 51
1

You could use the replace option. To replace A, B, C with 1,2,3 as per the below code.

df['city_new'] = df['City'].replace(['A','B','C'], [1,2,3])
ScottC
  • 157
  • 6
0

Your code was incorrect for two reasons:

  1. You used = instead of == to check for the string
  2. You need to state the equivalent of an 'else' clause if none of the logic statement are true, this is the value 4 in the code below.

Your code should look like this:

df['City_New'] = np.where(df['City']=='A', 1, np.where(df['City']=='B', 2, np.where(df['City']=='C', 3, 4)))