Search code examples
pandasdataframeif-statementrowmultiplication

Pandas with a condition select a value from a column and multiply by scalar in new column, row by row


A value in 'Target_Labels' is either 0.0,1.0,2.0 in float64.

Based on this value, I would like to look up a value in one of three columns 'B365A','B365D','B365H' and multiply this value by 10 in a new column. This operation needs to be row wise throughout the entire DataFrame.

I have tried many combinations but nothing seem to work...

final['amount'] = final['Target_Labels'].apply((lambda x: 'B365A' * 10 if x==0.0 else ('B365D' * 10 if x ==1 else 'B365H' * 10))
 
def prod(x, var1, var2, var3, var4):

    if (x[var4])==0:
         x[var3]*10
    elif (x[var4])==1:
        x[var1]*10
    else:
        x[var2]*10
    return x
final['montant'] = final.apply(lambda x: prod(x, 'B365D', 'B365H','B365A', 'Target_Labels'), axis=1)

I'm new to Pandas and any help is welcome...


Solution

  • Use numpy to indexing and get individual cells:

    • array = final.values
    • row = range(len(df))
    • col = final['Target_Labels'] - 1
    >>> final
       B365A  B365D  B365H  Target_Labels
    0     11     12     13              1
    1     11     12     13              2
    2     11     12     13              3
    
    >>> final['amount'] = final.values[(range(len(final)),
                                        final['Target_Labels'] - 1)] * 10
    
    >>> final
       B365A  B365D  B365H  Target_Labels  amount
    0     11     12     13              1     110
    1     11     12     13              2     120
    2     11     12     13              3     130