根据数据框中的2列提取信息并创建一个新列-Python

我的数据框中有2列。 “成人”代表酒店房间中的成年人数,“儿童”代表房间中的儿童数。

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".

问题是我有大量数据,并且我希望代码能够快速运行。

有什么建议吗?这是我需要的输入和输出的样本。

adult children   family_status

2       0       "Couple without children"     
2       0       "Couple without children"
2       1       "Couple with one child"
评论
情断心却相
情断心却相

Use np.select

condlist = [(df['adults']==2 & df['children']==0),(df['adults']==2 & df['children']==1)]
choicelist = ['couple with no children','couple with 1 child']
df['out'] = np.select(condlist,choicelist)
点赞
评论