Reputation: 113
I have 2 columns in my data frame. “adult” represents the number of adults in a hotel room and “children” represents the number of children in a room.
I want to create a new column based on these two.
For example if df['adults'] == 2 and df[‘children’]==0
the value of the new column would be "couple with no children".
And if the df['adults'] = 2 and df[‘children’]=1
the value of the new column would be "couple with 1 child".
I have a big amount of data and I want the code to run fast.
Any advice? This is a sample of the inputs and the output that I need.
adult children family_status
2 0 "Couple without children"
2 0 "Couple without children"
2 1 "Couple with one child"
Upvotes: 1
Views: 486
Reputation: 20669
np.select
df
adult children
0 2 0
1 2 0
2 2 1
condlist = [(df['adults']==2) & (df['children']==0),(df['adults']==2) & (df['children']==1)]
choicelist = ['couple with no children','couple with 1 child']
df['family_status'] = np.select(condlist,choicelist,np.nan)
df
adult children family_status
0 2 0 couple with no children
1 2 0 couple with no children
2 2 1 couple with 1 child
Upvotes: 1
Reputation: 860
You can try:
df['family_status'] = df.apply(lambda x: 'adult with no child' if (x['adult']==2 and x['children']==0)
else ( 'adult with 1 child'
if (x['adult']==2 and x['children']==1) else ''), axis=1)
Hope this will help you!!
Upvotes: 1