There are 2 dataframes with 1 to 1 correspondence. I can retrieve an idxmax
from all columns in df1
.
Input:
JavaScript
x
5
1
df1 = pd.DataFrame({'ref':[2,4,6,8,10,12,14],'value1':[76,23,43,34,0,78,34],'value2':[1,45,8,0,76,45,56]})
2
df2 = pd.DataFrame({'ref':[2,4,6,8,10,12,14],'value1_pair':[0,0,0,0,180,180,90],'value2_pair':[0,0,0,0,90,180,90]})
3
4
df=df1.loc[df1.iloc[:,1:].idxmax(), 'ref']
5
Output: df1, df2 and df
JavaScript
1
22
22
1
ref value1 value2
2
0 2 76 1
3
1 4 23 45
4
2 6 43 8
5
3 8 34 0
6
4 10 0 76
7
5 12 78 45
8
6 14 34 56
9
10
ref value1_pair value2_pair
11
0 2 0 0
12
1 4 0 0
13
2 6 0 0
14
3 8 0 0
15
4 10 180 90
16
5 12 180 180
17
6 14 90 90
18
19
5 12
20
4 10
21
Name: ref, dtype: int64
22
Now I want to create a df which contains 3 columns
Desired Output df
:
JavaScript
1
4
1
ref max value corresponding value
2
12 78 180
3
10 76 90
4
What are the best options to extract the corresponding values from df2
?
Advertisement
Answer
Your main problem is matching the columns between df1
and df2
. Let’s rename them properly, melt both dataframes, merge and extract:
JavaScript
1
9
1
(df1.melt('ref')
2
.merge(df2.rename(columns={'value1_pair':'value1',
3
'value2_pair':'value2'})
4
.melt('ref'),
5
on=['ref', 'variable'])
6
.sort_values('value_x')
7
.groupby('variable').last()
8
)
9
Output:
JavaScript
1
5
1
ref value_x value_y
2
variable
3
value1 12 78 180
4
value2 10 76 90
5