1

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

我想根据这两个创建一个新列。例如,如果df['adults'] == 2 and df[‘children’]==0新列的值是“没有孩子的夫妇”。如果df['adults'] = 2 and df[‘children’]=1新列的值是“有 1 个孩子的夫妇”。

我有大量数据,我希望代码快速运行。

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

adult children   family_status

2       0       "Couple without children"     
2       0       "Couple without children"
2       1       "Couple with one child"
4

2 回答 2

1

采用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
于 2020-05-23T15:34:23.643 回答
1

你可以试试:

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)

希望对你有帮助!!

于 2020-05-23T15:54:00.390 回答