使用Python的滞后经验让我很难完成这个循环。这是数据帧https://1drv.ms/u/s!AlPw3RIiTz1ChRo9YO4kYCI7n0r0?e=OeiLgx
我想再增加一列('customer_type'),其中包含字符串描述(可以是'New_Guest','reptative with cancelations','reptative NO cancelations')。需满足的条件:
我尝试了第一个条件,但没有成功
for i in df_test.loc[:,'customer_type'] : if ((df_test['is_repeated_guest']==0) & (df_test['previous_cancellations']==0)).all() : df_test.loc[:,'customer_type'] = 'New Guest' else : df_test.loc[:,'customer_type'] = 0
有人有什么建议吗
您可以过滤df并设置列值,而无需使用带有
df.loc[condition, column_name] = new_value
的循环。在你的情况下,它看起来像这样您还可以考虑在创建新列时添加默认的“CuuleType类型”,以防在列出的三种情况下出现情况。例如,您是否曾经有过“is_repeaded_guest”==0而“previous_cancellations”==1
相关问题 更多 >
编程相关推荐