亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號