0

I tried different solutions refering to this problem but nothing worked. I have 3 columns of type float and I want to compare the moltiplication of those with another column row by row. Original dataframe looks like this:

+------+------+------+--------+
| Col1 | Col2 | Col3 | Result |
+------+------+------+--------+
|  4.5 |  2.0 |  2.0 |   18.0 |
|  2.3 |  1.0 |  3.5 |   8.05 |
|  2.5 |  5.2 |  3.0 |    5.0 |
+------+------+------+--------+

Third row is obviously wrong, first and second rows are right. So I created a new column named Check, which should tell, if column Result is the multiplication of col1,col2 and col3.

df['Check'] = np.where((df.col1*df.col2*df.col3) != falseVol.VOLUM,'Result is wrong', 'Result is right')

Because I got wrong results with this, I tried several ways to multiply columns and created another column to store the calculated results in:

df['calculated'] = df['col1'] * df['col2'] * df['col3']
df['calculated'] = df['col1'].multiply(df['col2'] * df['col3'], axis=0)

Each gives me matching results but the check does not work.

How does row by row mulitplication of several columns and comparing work? Thank you.

MaMo
  • 569
  • 1
  • 10
  • 27

1 Answers1

3

Use df.prod/np.prod to multiply your columns (effectively generalising to many columns), and use np.isclose to perform elementwise floating comparison with df.Result.

np.isclose(df.iloc[:, :-1].prod(axis=1), df.Result)

Or,

np.isclose(np.prod(df.iloc[:, :-1], axis=1), df.Result)

array([ True,  True, False])

Where,

df.iloc[:, :-1].prod(axis=1)

0    18.00
1     8.05
2    39.00
dtype: float64

Note that performing a direct eq comparison yields an incorrect result -

df.iloc[:, :-1].prod(axis=1).eq(df.Result)

0     True
1    False   # incorrect
2    False
dtype: bool

This is a consequence of floating point inaccuracies.

cs95
  • 379,657
  • 97
  • 704
  • 746