i have train dataset which has 12 columns.
I want to select Cabin column rows according to Pclass column's value 1. And then replace value of selected rows of Cabin column with 1.
i did following code but it replace all values of cabin column with 1 even NaN values replace by 1.How i can replace only selected rows?
train['Cabin'] =train[train['Pclass']==1]['Cabin']=1
You can select by loc
with condition rows of column Cabin
and set to scalar:
train.loc[train['Pclass'] == 1, 'Cabin'] = 1
And your code replace all values to 1
because is is same as:
train['Cabin'] = 1
Sample:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With