lundi 2 décembre 2019

(Python) if statement logic error for list comprehension

I am trying to multiply -1 to all credit charges while keeping debit charges the same using list comprehension but, the if function was ignored. I suspect there is a syntax error here if transactions['Transaction Type'] is "debit" but I cannot figure out why.

Here is the code

transactions['Value'] = [i if transactions['Transaction Type'] is "debit" else i*-1 for i in transactions['Amount']]

print(transactions.loc[:, ['Amount','Transaction Type','Value']])

The result contains all negative values on Value:

             Amount Transaction Type    Value
Date                                         
2018-12-06    19.57            debit   -19.57
2018-12-06    27.87            debit   -27.87
2018-12-06     6.25            debit    -6.25
2018-12-06    14.38            debit   -14.38
2018-12-06    15.60            debit   -15.60
...             ...              ...      ...
2019-11-30    10.59            debit   -10.59
2019-11-30    51.32            debit   -51.32
2019-11-30   634.51           credit  -634.51
2019-12-01  4432.00            debit -4432.00
2019-12-01     5.00            debit    -5.00

Aucun commentaire:

Enregistrer un commentaire