I have next DataFrame in Pandas:
data1 = pd.DataFrame(data=[[1, 10, 100], [2,2,200],[3,3,300], [4,40,400]],
columns=['A', 'B', 'C'])
Here it is:
A B C
0 1 10 100
1 2 2 200
2 3 3 300
3 4 40 400
What I want to do: find rows, where 'A' == 'B'
and replace for this rows column 'C'
value.
So what I want to get:
A B C
0 1 10 100
1 2 2 -1
2 3 3 -1
3 4 40 400
What I already tried:
data1[data1['A']==data1['B']]
So I find necessary rows. Now I try to replace values in this rows:
data1[data1['A']==data1['B']]['C'] = -1
But data1 is the same! Looks like this difficult chain indexing goes wrong or all this operation returns copy of dataframe. But I can't save it to new dataframe, because I used =
in last command, I just can't write newdf = data1[...] = -1
.
I found also replace
function:
data1.replace(data1[data1['A']==data1['B']], "-1")
But it replace all values in row, when I need only last column:
A B C
0 1 10 100
1 -1 -1 -1
2 -1 -1 -1
3 4 40 400
P.S. I know I can do it by using for loop. But I try to find better (more elegant) solution.