Home > Blockchain >  How to get the maximum value of the same columns from two dataframe
How to get the maximum value of the same columns from two dataframe

Time:02-17

I have two dataframe, df1 and df2 as below. I am trying to create a new dataframe that will show the maximum score for each student across subjects. For example, in the new dataframe, the match_score for George would be 63 (which is the maximum match_score in df1 and df2).

Is there a way to do this? Any advices and suggestion will be greatly appreciated.

import pandas as pd

df1 = { 'student':['George','Andrea','micheal','Ann',
        'maggie','Ravi','Xien','Jalpa'],
'match_score':[63,42,56,70,38,78,84,99],
'ela_score':[43,74,41,82,69,46,70,98]}


df2 = { 'student':['George','Andrea','micheal', 'Matt',
        'maggie','Ravi','Xien','Jalpa'],
'match_score':[62,47,55,74,32,77,86,77],
'ela_score':[45,78,44,89,66,49,72,73]}

df1=pd.DataFrame(df1)
df2=pd.DataFrame(df2)

CodePudding user response:

Use pd.concat with Groupby.max:

In [1293]: df = pd.concat([df1, df2]).groupby(level=0).max()

In [1294]: df
Out[1294]: 
   student  match_score  ela_score
0   George           63         45
1   Andrea           47         78
2  micheal           56         44
3     Matt           74         89
4   maggie           38         69
5     Ravi           78         49
6     Xien           86         72
7    Jalpa           99         98

CodePudding user response:


data=pd.concat([df1, df2])

data.match_score.max()

99

  • Related