Reputation: 97
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)?
Upvotes: 4
Views: 2879
Reputation: 2659
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)
Upvotes: 8