lundi 14 octobre 2019

pandas iterrows() comparison only writing last result to all rows

The following code is supposed to iterate over a dataframe & write to the 'Valid' column. It is only writing the last value to all rows in 'Valid'.

for index, row in lxrx.iterrows():
time_cur = (row['time'])
low_val = csvm.loc['LXRX', time_cur].low
price = (row['Price'])
if price > low_val:
    lxrx['Valid'] = price
else:
    lxrx['Valid'] = 'Invalid'

To test I used: print(price) & print(low_val)

it output what I expect - each price then each low_val:

3.13  2.8    2.1  3.06    3.36  3.06

It Outputs the below left, instead of the below right as I wanted.

Index Valid     rather than     Index Valid
0     3.36                      0     3.13
1     3.36                      1     2.10
2     3.36                      2     3.36  

I even tried nesting another for loop inside that one so that I could refer to each row in Valid but that did the same thing. How can I get it to correctly write each value?

Aucun commentaire:

Enregistrer un commentaire