OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get max of row in column in dataframes and keep information from different row

  • Thread starter Thread starter Regina Phalange
  • Start date Start date
R

Regina Phalange

Guest
Hope some one can help me with this. I have several dataframes that contains a value a corresponding id and name. For each id I want the max value for all dataframes but I must also now for which name this max value occured.

The example only shows two dataframes but I really have about a 100 which also contains about a 100 id's so can't just take the max and then check which values are contained in one of the dataframes and take the name from there.

I have tried with groupby but can not get it to work.

Code:
import pandas as pd
d1 = pd.DataFrame()
d2 = pd.DataFrame()
d1['id'] = [1,2,3,4,5]
d2['id'] = [1,2,3,4,5]
d1['max'] = [10,20,30,40,50]
d2['max'] = 25
d1['name'] = 'name_1'
d2['name'] = 'name_2'
df_max_list=[d1, d2]
df_max_out=pd.concat(df_max_list).groupby(level=0).max()

Got the result:

Code:
    id  max name
0   1   25  name_2
1   2   25  name_2
2   3   30  name_2
3   4   40  name_2
4   5   50  name_2

Expected result:

Code:
    id  max name
0   1   25  name_2
1   2   25  name_2
2   3   30  name_1
3   4   40  name_1
4   5   50  name_1
<p>Hope some one can help me with this. I have several dataframes that contains a value a corresponding id and name. For each id I want the max value for all dataframes but I must also now for which name this max value occured.</p>
<p>The example only shows two dataframes but I really have about a 100 which also contains about a 100 id's so can't just take the max and then check which values are contained in one of the dataframes and take the name from there.</p>
<p>I have tried with groupby but can not get it to work.</p>
<pre><code>import pandas as pd
d1 = pd.DataFrame()
d2 = pd.DataFrame()
d1['id'] = [1,2,3,4,5]
d2['id'] = [1,2,3,4,5]
d1['max'] = [10,20,30,40,50]
d2['max'] = 25
d1['name'] = 'name_1'
d2['name'] = 'name_2'
df_max_list=[d1, d2]
df_max_out=pd.concat(df_max_list).groupby(level=0).max()
</code></pre>
<p>Got the result:</p>
<pre><code> id max name
0 1 25 name_2
1 2 25 name_2
2 3 30 name_2
3 4 40 name_2
4 5 50 name_2
</code></pre>
<p>Expected result:</p>
<pre><code> id max name
0 1 25 name_2
1 2 25 name_2
2 3 30 name_1
3 4 40 name_1
4 5 50 name_1
</code></pre>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top