Based on my question here and here I want to sort a list of pandas dataframes and based on the desired order (here len) I want to change the values of the idx variable in the same way as the values of lst are changed. Means if lst = [df1, df2, df3] and idx = [1,2,3] and the ordered list (by len) is lst_new = [df3, df1, df2], then idx_new = [3,1,2]. A small example to illustrate my problem is:
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['a', 'b', 'c'])
df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [11, 12, 13]]),
columns=['a', 'b', 'c'])
df3 = pd.DataFrame(np.array([[1, 2, 3], ['x', 'y', 'z']]),
columns=['a', 'b', 'c'])
idx = [1,2,3]
lst = []
lst.append(df1)
lst.append(df2)
lst.append(df3)
lst = sorted(lst, key=len)
test = [i for j, i in sorted(zip(lst, idx))]
print(test)
gets the error message:
ValueError: Can only compare identically-labeled DataFrame objects