I'm trying to calculate new values in a column whose values are cross-referenced to another column.
>>> import pandas as pd
>>> df = pd.DataFrame( {"A":[0., 100., 80., 40., 0., 60.],
"B":[12, 12, 3, 19, 3, 19]} )
>>> df
A B
0 0.0 12
1 100.0 12
2 80.0 3
3 40.0 19
4 0.0 3
5 60.0 19
I want to find all values in column A that are 0, find out the corresponding value in column B, then change all column A values that have the same column B value, according to some function. For instance in the example above I would like to change the first two values of column A, df.A[0]
and df.A[1]
, respectively 0. and 100., into 0.5 and 99.5, because df.A[0]
is 0. and it has the same value df.B[0] = 12
in column B as df.B[1] = 12
.
df
A B
0 0.5 12
1 99.5 12
2 79.5 3
3 40.0 19
4 0.5 3
5 60.0 19
I tried chaining loc, aggregate, groupby and mask functionalities, but I'm not succeeding. Is the only way through a for loop?
EDIT: Broadened example to better illustrate intent.