[英]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.