DataFrame example (df):
a b c d e
1 2 O 3 f
2 1 B 5 b
1 5 B 5 d
3 2 O 3 e
Desired output: If c equals 0, then I wish the output is a - b, if c equals B, then I wish the output to be b - a. I want the results stored in an aditional column, named f.
a b c d e f
1 2 O 3 f -1
2 1 B 5 b -1
1 5 B 5 d 4
3 2 O 3 e 1
Note: I am not looking for any solution, it needs to be fast. A working solution is posted here, but is not fast enough.
What has been tried:
df.apply(lambda x: x.a - x.b if x.c == 'O' else x.b - x.a)
Error shown:
Series object has no attribute 'c'.
Also:
[data[1].a-data[1].b if data[1].c == 'O' else data[1].b - data[1].a for data in df.iterrows()]
This solution works, however, it is very inefficient. I would like something that runs faster.
Aucun commentaire:
Enregistrer un commentaire