Issue
If I have a dataframe as follows:
import pandas as pd
df = pd.DataFrame({
'items': ['countryName', 'stateName', 'currencyName', 'companyName'],
'code': ['NK', 'NK', 'NK', 'NK']
})
print(df)
items code
0 countryName NK
1 stateName NK
2 currencyName NK
3 companyName NK
How can transform NK under several conditions, for instance, if its items is "countryName", change NK to North Korea, if its items is "stateName", change NK to "North Kingstown" so on and so forth. Please note this is only part of dataframe. Thanks.
df = pd.DataFrame({
'items': ['countryName', 'stateName', 'currencyName', 'companyName'],
'code': ['North Korea', 'North Kingstown', 'Norwegian krone', 'Northrup-King']
})
print(df)
items code
0 countryName North Korea
1 stateName North Kingstown
2 currencyName Norwegian krone
3 companyName Northrup-King
Solution
You can use np.where on the DF. It's a bit dirty and I'm sure someone else can give you a cleaner solution but it works.
df['code'] = np.where((df['code'] == 'NK') & (df['items'] == 'countryName'),
'North Korea',
df['code'])
df['code'] = np.where((df['code'] == 'NK') & (df['items'] == 'stateName'),
'North Kingstown',
df['code'])
... add the rest of the examples
How does it work:
- np.where((condition one) & (condition two) & (more conditions)...
- value to set for column 'code' if conditions are met, e.g. North Korea
- keep old value (NK) if conditions are not met
EDIT: Addition of simple dynamic version
replace_dict = {'North Korea':['NK','countryName'],'North Kingstown':['NK','stateName']}
for key in replace_dict.keys():
df['code'] = np.where((df.code == replace_dict[key][0]) & (df['items'] == replace_dict[key][1]),
key,
df['code'])
Answered By - David
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.