Reputation: 637
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
df["city-north"] = df['city']
where state = "Illinois"
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
Upvotes: 1
Views: 1095
Reputation: 60
I think your code should work fine.
df.loc[df['state'] == 'Illinois', 'city-north'] = df['city']
df.loc[df['state'] != 'Illinois', 'city-south'] = df['city']
I even tried it with multiple conditions and new inputs that were not in the original dataframe and still worked as follows:
df.loc[(df['state'] == 'Illinois') & (df['city'] =='Chicago'), 'city-north'] = 'New Entry']
df.loc[(df['state'] != 'Illinois') & (df['city'] =='Phoenix '), 'city-south'] = 'Another new entry'
Kindly note that the second part of my answer is to show more clarity with multiple conditions and it still worked. It can be used to add new columns with new entries.
Upvotes: 0
Reputation: 862441
For me working well, if no match conditions are created missing values:
df.loc[df['state'] == 'Illinois', 'city-north'] = df['city']
df.loc[df['state'] != 'Illinois', 'city-south'] = df['city']
print (df)
person city state country city-north city-south
0 A Chicago Illinois USA Chicago NaN
1 B Phoenix Arizona USA NaN Phoenix
2 C San Diego California USA NaN San Diego
If need empty values strings for not matched rows:
df['city-north'] = np.where(df['state'] == 'Illinois', df['city'], '')
df['city-south'] = np.where(df['state'] != 'Illinois', df['city'], '')
print (df)
person city state country city-north city-south
0 A Chicago Illinois USA Chicago
1 B Phoenix Arizona USA Phoenix
2 C San Diego California USA San Diego
Upvotes: 1