0

Hi I am new to dataframes, and I wanted to know how could I sort a list of 3 dataframes, where each dataframe has a size of n x n. For example like this:

df1 = pd.DataFrame([[1,2,3],[6,7,8],[11,12,13]], columns = ['a,','b','c'])
df2 = pd.DataFrame([[6,1,9],[0,3,2],[12,14,10]], columns = ['a,','b','c'])
df3 = pd.DataFrame([[1,4,8],[2,1,1],[10,16,15]], columns = ['a,','b','c'])
j = [df1,df2,df3]

and I wanted to sort j by the number in the first row and second column of each dataframe in descending order. So j = [df1,df2,df3] becomes j = [df3,df1,df2]

1 Answer 1

1

Try sorted, giving it a key function to pull out the entry in the first row, second column:

sorted(j, key=lambda x: x.iloc[0,1], reverse=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I was stuck on this for like an entire day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.