为了账号安全,请及时绑定邮箱和手机立即绑定

Pandas 中的类别值分配

Pandas 中的类别值分配

喵喵时光机 2021-12-09 15:51:31
在我的数据框中,我有两列。Emp_id 和城市。数据框的总大小为 320 万个,包含多个城市名称。数据框看起来像 -emp_id         city  2            New York  3            Houston  6            Dallas  7            New York  11           Dallas  12           Austin  13           San Jose  14           Boston  15           Boston  16           Columbus  24           Austin  30           Austin我的最终输出看起来像 -emp_id         city              present  2            New York             1  3            Houston              0  6            Dallas               1  7            New York             1  11           Dallas               1  12           Austin               0  13           San Jose             0  14           Boston               1  15           Boston               1  16           Columbus             0  24           Austin               0  30           Austin               0到目前为止我已经做了 -df['present'] = np.where(df.loc[df['city'].isin(['New York','Dallas','Boston'])],1,0)我只想将 3 个城市视为“1”,其余城市为“0”
查看完整描述

1 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

你可以做:

df['present'] = np.where(df['city'].isin(['New York','Dallas','Boston']),1,0)


查看完整回答
反对 回复 2021-12-09
  • 1 回答
  • 0 关注
  • 220 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号