Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort list of dataframes in python by highest value in shared column

Tags:

python

pandas

Is there a way to sort a list of pandas dataframes by the highest value of one of the columns in the dataframes (the columns are shared by the dataframes)?

like image 514
Ignacio Francisco Fuentes San Avatar asked Mar 09 '23 14:03

Ignacio Francisco Fuentes San


1 Answers

from pandas import DataFrame
import pandas as pd
dict1 = {"id":[1,2,3],"age":[10,20,60]}
dict2 = {"id":[4,5,6],"age":[10,20,40]}

df1 = DataFrame.from_dict(dict1)
df2 = DataFrame.from_dict(dict2)

dflist = [df1,df2]


sorteddflist= sorted(dflist,key=lambda x:x["age"].max(axis=0))
for i in sorteddflist:
    print(i)
like image 127
Himaprasoon Avatar answered Apr 12 '23 22:04

Himaprasoon