Reputation: 1998
In my df
I have a column for each entity below (Grubhub, Toasttab, Tenk) and it says either yes or no in the value of that column for each row,
I have below code such as:
df['Grubhub'] = df[['On GrubHub or Seamless?']].apply(lambda x: any(x == 'Yes'), axis = 1)
df['ToastTab'] = df[['On ToastTab?']].apply(lambda x: any(x == 'Yes'), axis = 1)
df['Tenk'] = df[['On Tenk?']].apply(lambda x: any(x == 'Yes'), axis = 1)
df['Udemy'] = df[['On Udmey?']].apply(lambda x: any(x == 'Yes'), axis = 1)
df['Postmates'] = df[['On Postmates?']].apply(lambda x: any(x == 'Yes'), axis = 1)
df['Doordash'] = df[['On DoorDash?']].apply(lambda x: any(x == 'Yes'), axis = 1)
df['Google'] = df[['On Goole?']].apply(lambda x: any(x == 'Yes'), axis = 1)
This gives me a new column for each entity ( Grubhub, Toasttab, Tenk ) and that column gives a true of false value, is there a more efficient method where I can do all of these in one line of code or function? Thanks for help in advance
Upvotes: 1
Views: 97
Reputation: 11242
You can create a column map and apply a function
inside loop
:
columns_map = (
('Grubhub', 'On GrubHub or Seamless?'),
('ToastTab', 'On ToastTab?'),
('Tenk', 'On Tenk?'),
# etc ...
)
for new_col, alias in columns_map:
df[new_col] = df[alias].apply(lambda x: x == 'Yes')
# also you can easily remove aliases columns:
# df = df.drop(columns=[alias])
Or you can set value into original column and rename if you need(without drop()
):
for new_col, alias in columns_map:
df[alias] = df[alias].apply(lambda x: x == 'Yes')
df.rename(
columns={alias: new_col for new_col, alias in columns_map},
inplace=True
)
Upvotes: 2