在我的數(shù)據(jù)框中,我有兩列。Emp_id 和城市。數(shù)據(jù)框的總大小為 320 萬個,包含多個城市名稱。數(shù)據(jù)框看起來像 -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到目前為止我已經(jīng)做了 -df['present'] = np.where(df.loc[df['city'].isin(['New York','Dallas','Boston'])],1,0)我只想將 3 個城市視為“1”,其余城市為“0”
1 回答

慕容森
TA貢獻(xiàn)1853條經(jīng)驗 獲得超18個贊
你可以做:
df['present'] = np.where(df['city'].isin(['New York','Dallas','Boston']),1,0)
添加回答
舉報
0/150
提交
取消