Home > Blockchain >  How to multiply two columns together with a condition applied to one of the columns in pandas python
How to multiply two columns together with a condition applied to one of the columns in pandas python

Time:12-12

Here is some example data:

data = {'Company': ['A', 'B', 'C', 'D', 'E', 'F'],
        'Value': [18700, 26000, 44500, 32250, 15200, 36000],
        'Change': [0.012, -0.025, -0.055, 0.06, 0.035, -0.034]
       }
df = pd.DataFrame(data, columns = ['Company', 'Value', 'Change'])
df

Company Value   Change
0   A   18700   0.012
1   B   26000   -0.025
2   C   44500   -0.055
3   D   32250   0.060
4   E   15200   0.035
5   F   36000   -0.034

I would like to create a new column called 'New Value'. The logic for this column is something along the lines of the following for each row:

  • if Change > 0, then Value (Value * Change)
  • if Change < 0, then Value - (Value * (abs(Change)) )

I attempted to create a list with the following loop and add it to df as a new column but many more values than expected were returned when I expected only 5 (corresponding with the number of rows in df).

lst = []

for x in df['Change']:
    for y in df['Value']:
        if x > 0:
            lst.append(y   (y*x))
        elif x < 0:
            lst.append(y - (y*(abs(x))))
print(lst)

It would be great if someone could point out where I've gone wrong, or suggest an alternate method :)

CodePudding user response:

Your two conditions are actually identical, so this is all you need to do:

df['New Value'] = df['Value']   df['Value'] * df['Change']

Output:

>>> df
  Company  Value  Change  New Value
0       A  18700   0.012    18924.4
1       B  26000  -0.025    25350.0
2       C  44500  -0.055    42052.5
3       D  32250   0.060    34185.0
4       E  15200   0.035    15732.0
5       F  36000  -0.034    34776.0

Or, slightly more consisely:

df['New Value'] = df['Value'] * df['Change'].add(1)

Or

df['New Value'] = df['Value'].mul(df['Change'].add(1))
  • Related