4

I tried looking for an answer but couldn't find it. If someone has a link, all the better. My question goes as follows.

  1. I have a sheet I am reading in pandas which has numerous columns with values in it.
  2. There are three calculations I need to run which use bits from different columns at a time.
  3. I need to return the max of those calculations in one bit of code, which will then be added to a new column.

I'm having problems with number 2.

Here is what my code would look like.

df = read_csv('file.csv')

df['Get New'] = maximum(df[Long] - df[Short], df[Long] + df[Short], df[Long] / df[Short])

df.to_csv('newFile.csv', index=False)

I know maximum doesn't work in this scenario but I can't seem to find what would. Any help is appreciated. Thanks!

EDIT: Here is the solve.

df['Get New'] = np.maximum(df['Long'] - df['Short'],  df['Long'] + df['Short'])
df['Get New'] = np.maximum(df['Get New'], df['Long'] / df['Short'])
Ian Lang
  • 45
  • 4

1 Answers1

3

Use np.maximum with reduce:

import numpy as np


df = pd.DataFrame({
         'Long':[7,8,9,4,2,3],
         'Short':[1,3,5,7,1,7],
})

df['Get New'] = np.maximum.reduce([df['Long'] - df['Short'], 
                                   df['Long'] + df['Short'], 
                                   df['Long'] / df['Short']])
print (df)
   Long  Short  Get New
0     7      1      8.0
1     8      3     11.0
2     9      5     14.0
3     4      7     11.0
4     2      1      3.0
5     3      7     10.0

Alternative is use np.maximum for pairs only:

df['Get New'] = np.maximum(df['Long'] - df['Short'],  df['Long'] + df['Short'])
df['Get New'] = np.maximum(df['Get New'], df['Long'] / df['Short'])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252