I have a dataframe with a column that includes the names of boroughs in New York City (Manhattan, Brooklyn, etc.). I want to create another column, 'borough_num', that assigns a number for each borough (Manhattan -> 1, Brooklyn -> 2, Queens -> 3, Staten Island -> 4, Bronx -> 5, Other -> 0).
However, in the Borough column, some rows contain numbers in front of the borough name (e.g. instead of "Bronx" I have "07 Bronx"). Because this "07 Bronx" is still part of the Bronx borough, it should also be assigned to the same value "5" as "Bronx". I therefore need to create a dictionary that assigns the number 5 to a string that contains the word "Bronx". The same for each Borough. Any clue on how to do this? I am new to python!
This is what I had before noticing the cells with the numbers:
df['Borough'] = df['Borough'].fillna('OTHER')
borough_dict = {'MANHATTAN':1, 'BROOKLYN':2, 'QUEENS': 3, 'STATEN ISLAND': 4, 'BRONX': 5, 'OTHER':6}
df['borough_num'] = df['Borough'].apply(lambda x:0 if borough_dict.get(x) == None else borough_dict.get(x))

str.__contains__('BRONX')orregex expressions- Morseremodule in python (included in all base installs) and use regex101.com to play around with your expression. Welcome to StackOverflow! - divibisan