I have a pandas dataframe where I need to create new columns based on values from other columns in dataframe. Here is the dataframe
| person | city | state | country |
|---|---|---|---|
| A | Chicago | Illinois | USA |
| B | Phoenix | Arizona | USA |
| C | San Diego | California | USA |
I want to create two new columns based on the values in state
- Create new column
df["city-north"] = df['city']where state ="Illinois" - Create new column
df["city-south"] = df['city']where state is not equal to"Illinois"
I tried
df.loc[((df['state'] == 'Illinois')), 'city-north'] = df['city']
df.loc[((df['state'] != 'Illinois')), 'city-south'] = df['city']
But second line of code where not equal to condition is there does not create 'city-south' column. Please help