简体   繁体   中英

how to replace values of selected row of a column in panda's dataframe?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM