I have the following Python Pandas Dataframe (8 rows):
City Name New York Long Beach Jamestown Chicago Forrest Park Berwyn Las Vegas Miami
I would like to add a new Column (Branch Name) based on City Name as below:
City Name Branch Name New York New York Long Beach New York Jamestown New York Chicago Chicago Forrest Park Chicago Berwyn Chicago Las Vegas Las Vegas Miami Miami
How do I do that?
You can use .map()
. City names not in the dictionnary will be kept.
df["Branch Name"] = df["City Name"].map({"Long Beach":"New York",
"Jamestown":"New York",
"Forrest Park":"Chicago",
"Berwyn":"Chicago",}, na_action='ignore')
df["Branch Name"] = df["Branch Name"].fillna(df["City Name"])