Rudra
Rudra

Reputation: 159

How to get maximum value from multiple columns

Problem description

my dataset looks like below

id   tre  f1   f2 f3 t1
1    2     0   1  1  er
2    3     1   0  0  po
3    abc   1   1  1  oo
4    yt    0   0  0  pp

I need to focus on f1,f2,f3.. create a new col based on f1,f2,f3....the values in the new col will be max(f1,f2,f3) what function should I use

Upvotes: 0

Views: 1676

Answers (3)

9mat
9mat

Reputation: 1234

Just use pd.DataFrame.max

# test data
from io import StringIO
data = StringIO('''id,tre,f1,f2,f3,t1
1,2,0,1,1,er
2,3,1,0,0,po
3,abc,1,1,1,oo
4,yt,0,0,0,pp''')
df = pd.read_csv(data)


df['maxf'] = df[['f1', 'f2', 'f3']].max(axis=1)

output:
   id  tre  f1  f2  f3  t1  maxf
0   1    2   0   1   1  er     1
1   2    3   1   0   0  po     1
2   3  abc   1   1   1  oo     1
3   4   yt   0   0   0  pp     0

Edited: to find max of all columns start with 'f':

cols = list(filter(lambda x: x.startswith('f'), df.columns))
df['maxf'] = df[cols].max(axis=1)

Upvotes: 1

Aqua 4
Aqua 4

Reputation: 881

Use pandas pandas.DataFrame.max

This should give you a new column with max values from all the 3 columns

df["max"] = df[["f1", "f2","f3"]].max(axis=1)

you can then run a max on this column to get max value

df['max'].max()

Upvotes: 1

vb_rises
vb_rises

Reputation: 1907

df['max'] = df.apply(lambda x : max(x['f1'], x['f2'], x['f3']), axis = 1)

Upvotes: 1

Related Questions