假设我有下面这个数据框,并且我想将所有非' united states' native_country更改为'Other',我可以使用pandas吗?代替做这个?如果不是,我该怎么办?
age education marital_status occupation race sex native_country
1 37 Non-grad Married Sales Other Female ?
2 39 HS-grad Married Sales Other Female Dominican-Republic
3 29 HS-grad Married Sales White Female United-States
4 64 HS-grad Married Sales White Female United-States
5 31 HS-grad Married Sales White Female United-States
6 53 HS-grad Married Sales White Female United-States
7 56 HS-grad Married Sales White Female United-States
8 28 Bachelors Married Sales White Female United-States
9 52 Some-college Married Sales White Female Canada
10 23 Some-college Married Sales White Female United-States
我想要的输出:
age education marital_status occupation race sex native_country
1 37 Non-grad Married Sales Other Female Other
2 39 HS-grad Married Sales Other Female Other
3 29 HS-grad Married Sales White Female United-States
4 64 HS-grad Married Sales White Female United-States
5 31 HS-grad Married Sales White Female United-States
6 53 HS-grad Married Sales White Female United-States
7 56 HS-grad Married Sales White Female United-States
8 28 Bachelors Married Sales White Female United-States
9 52 Some-college Married Sales White Female Other
10 23 Some-college Married Sales White Female United-States
using mask or loc
# using mask, when native country is not United-States, makes it other, else
# leave value as is
df['native_country'] = df['native_country'].mask(df['native_country'].ne('United-States'),'other')
df
或
# using loc, updated native country, where its not united-states
df.loc[df['native_country'].ne('United-States'), 'native_country'] = 'other'
df
age education marital_status occupation race sex native_country
1 37 Non-grad Married Sales Other Female other
2 39 HS-grad Married Sales Other Female other
3 29 HS-grad Married Sales White Female United-States
4 64 HS-grad Married Sales White Female United-States
5 31 HS-grad Married Sales White Female United-States
6 53 HS-grad Married Sales White Female United-States
7 56 HS-grad Married Sales White Female United-States
8 28 Bachelors Married Sales White Female United-States
9 52 Some-college Married Sales White Female other
10 23 Some-college Married Sales White Female United-States