I have a data frame to work on and I am performing several checks.
I am checking whether the repeating values under the columns "A", "B" and "C" present the same number but with opposite sign under column D.
| A | B | C | D | E |
|---|---|---|---|---|
| 1111 | AAA | 123 | 0.01 | comment to be replaced |
| 2222 | BBB | 456 | 5 | comment to be replaced |
| 3333 | CCC | 789 | 10 | don't do anything |
| 1111 | AAA | 123 | -0.01 | comment to be replaced |
| 2222 | BBB | 456 | -5 | comment to be replaced |
| 3333 | CCC | 789 | -9 | don't do anything |
Please see my code below. When I try replacing the comment under the column "E", it does not work. I am sure that I am doing something wrong. I am fully aware of the fact that I haven't written the code in the most efficient way, I am still a newbie. Would you be able to help me with both a more efficient way to achieve this and, out of curiosity, how this could be achieved if I decided to keep using this "non-efficient" way?
Thank you.
for i in range(0, len(df)-1):
for j in range(i+1, len(df)):
if (df['A'][i] == df['A'][j]) & (df['B'][i] == df['B'][j]) & (df['C'][i] == df['C'][j]) & (df['D'][i] + df['D'][j] = 0) :
df['E'][i] = 'it works!'