[英]How to correctly use panda's slice/replace with values form another column and then explode a row into two
[英]how to replace values of selected row of a column in panda's dataframe?
您可以通過選擇loc
與列的條件行Cabin
並設置為標量:
train.loc[train['Pclass'] == 1, 'Cabin'] = 1
並且您的代碼將所有值替換為1
因為它與:
train['Cabin'] = 1
樣品 :
train = pd.DataFrame({'Pclass':[1,2,3,1,2],
'Cabin':[10,20,30,40,50]})
print (train)
Cabin Pclass
0 10 1
1 20 2
2 30 3
3 40 1
4 50 2
train.loc[train['Pclass'] == 1, 'Cabin'] = 1
print (train)
Cabin Pclass
0 1 1
1 20 2
2 30 3
3 1 1
4 50 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.