Pi-thon
Pi-thon

Reputation: 41

Rename column based on other column value

I have a dataframe with the following columns:

Region | LA code | LA Name
-----------------------------------------
London |    201  | City of London
London |    202  | Camden
London |    203  | Greenwich
London |    204  | Hackney
London |    205  | Hammersmith and Fulham
London |    206  | Islington
London |    207  | Kensington and Chelsea
London |    208  | Lambeth
London |    209  | Lewisham
London |    210  | Southwark
London |    211  | Tower Hamlets
London |    212  | Wandsworth
London |    213  | Westminster
London |    301  | Barking and Dagenham
London |    302  | Barnet
London |    303  | Bexley
London |    304  | Brent
London |    305  | Bromley
London |    306  | Croydon
London |    307  | Ealing
London |    308  | Enfield
London |    309  | Haringey
London |    310  | Harrow
London |    311  | Havering
London |    312  | Hillingdon
London |    313  | Hounslow
London |    314  | Kingston upon Thames
London |    315  | Merton
London |    316  | Newham
London |    317  | Redbridge
London |    318  | Richmond upon Thames
London |    319  | Sutton
London |    320  | Waltham Forest

My question is what is a quick and simple way of renaming London to Inner London where the LA code falls within the range 201 - 213 and renaming London to Outer London where the LA code falls within the range 301 - 320?

Thanks.

Upvotes: 2

Views: 402

Answers (2)

jpp
jpp

Reputation: 164843

Using np.select, you can specify a list of conditions and values:

df = pd.DataFrame([['London', 201, 'City of London'],
                   ['London', 302, 'Barnet']],
                  columns=['Region', 'LA Code', 'LA Name'])

conditions = [df['LA Code'].between(201, 213), df['LA Code'].between(301, 320)]

values = ['Inner ' + df['Region'], 'Outer ' + df['Region']]

df['Region'] = np.select(conditions, values, df['Region'])

print(df)

         Region  LA Code         LA Name
0  Inner London      201  City of London
1  Outer London      302          Barnet

Note that the final argument to np.select is the default argument, used when none of the conditions supplied are applicable.

Upvotes: 1

cs95
cs95

Reputation: 403198

Both questions are answered by pd.Series.between.

m = df['LA Code'].between(201, 213)
df.loc[m, 'Region'] = 'Inner ' + df.loc[m, 'Region']
# df.loc[m, 'Region'] = df.loc[m, 'Region'].radd('Inner ')

And,

m = df['LA Code'].between(301, 320)
df.loc[m, 'Region'] = 'Outer ' + df.loc[m, 'Region']

Upvotes: 1

Related Questions