Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substitute values in dataframe

Tags:

python

pandas

I have a dataframe with 3 columns(A,B,C). I wanted to update column C with column A when column B=22. I have written update statement like this, but it's updating NaN for non matching rows. Could you tell me how to update data in dataframe?

df = pd.DataFrame(data=[[10,20,30],[11,21,31],[12,22,32]], columns=['A','B','C'])
df.C = df[df.B==22].A
like image 392
ramreddy bolla Avatar asked Dec 18 '22 12:12

ramreddy bolla


1 Answers

Let us try mask

df.C.mask(df.B==22, df.A,inplace=True)
df
    A   B   C
0  10  20  30
1  11  21  31
2  12  22  12
like image 176
BENY Avatar answered Dec 29 '22 22:12

BENY