0

I have a dataset that looks like this:

+------+---+----+-----+---+---+-------+
| Name | A | B  | ... | X | Y | maxXY |
+------+---+----+-----+---+---+-------+
| John | 3 | 6  |     | 3 | 1 |       |
+------+---+----+-----+---+---+-------+
| Will | 2 | 3  |     | 4 | 4 |       |
+------+---+----+-----+---+---+-------+
| Dave | 2 | 7  |     | 3 | 2 |       |
+------+---+----+-----+---+---+-------+
| Pete | 1 | 21 |     | 2 | 3 |       |
+------+---+----+-----+---+---+-------+

I would like to calculate the row-wise maximum value from a selection of columns which I would reference using the column id. So something like this:

target_columns = [45,46] # X = 45, Y = 46
dataframe['maxXY'] = ...

Many thanks,

BYZZav
  • 1,418
  • 1
  • 19
  • 35
  • Don't agree that its a duplicate since I was asking to reference the column by id and not naming columns since I have over 15 columns to work with. – BYZZav Jun 19 '19 at 01:30

3 Answers3

1

This seems to work:

dataframe['maxXY'] = np.max(dataframe.iloc[:, target_columns],axis=1)
BYZZav
  • 1,418
  • 1
  • 19
  • 35
1

We usually using pandas.DataFrame.max

dataframe['maxXY'] = dataframe.iloc[:, target_columns].max(1)
BENY
  • 317,841
  • 20
  • 164
  • 234
0

I believe np.max(dataframe[[dataframe.index[45],dataframe.index[46]].iloc[row_num].values) may produce the desired results.

TheLoneDeranger
  • 1,161
  • 9
  • 13
  • 1) row_num means I have to iterate through all the rows 2) this means I also have to have 'dataframe.index[..]' for each column Surely there is a more elegant way than this – BYZZav Jun 18 '19 at 23:41
  • I see what you mean. Perhaps transpose your df and index rows? `df = pd.DataFrame() df['a'] = np.random.rand(50) df['b'] = np.random.rand(50) new_panda = df.transpose() np.max(new_panda.iloc[0])` – TheLoneDeranger Jun 18 '19 at 23:54
  • 1
    np.max is the answer, but using iloc[:,target_cols] -see above. thanks for your help though :) – BYZZav Jun 19 '19 at 00:07