J'ai un pandas DataFrame construit comme ceci:

l1 = [[[1,2], [3,5]], [[3,2], [5,2]]]
l2 = [[[9,2], [4,5]], [[4,3], [2,7]]]
l3 = [[[3,3], [4,4]], [[6,3], [4,6]]]
l4 = [[[4,4], [3,5]], [[2,5], [3,4]]]
small_df = pd.DataFrame({'col': [l1, l2, l3, l4]})

print(small_df)
col
0   [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1   [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]
2   [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]
3   [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]

Je voudrais d'abord trier la liste interne de points pour que [[9, 2], [4, 5]] devienne [[4, 5], [9, 2]], puis trier la liste externe pour que [[[4, 5], [9, 2]], [[2, 7], [4, 3]]] devienne [[[2, 7], [4, 3]], [[4, 5], [9, 2]]]. J'ai trouvé comment trier la liste externe comme ceci:

small_df["sorted"] = small_df["col"].apply(sorted)
small_df
    col                                     sorted
0   [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]    [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1   [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]    [[[4, 3], [2, 7]], [[9, 2], [4, 5]]]
2   [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]    [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]
3   [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]    [[[2, 5], [3, 4]], [[4, 4], [3, 5]]]

Mais comment puis-je d'abord trier la liste interne? Je voudrais finir avec ceci:

col                                         goal
0   [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]    [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1   [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]    [[[2, 7], [4, 3]], [[4, 5], [9, 2]]]
2   [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]    [[[3, 3], [4, 4]], [[4, 6], [6, 3]]]
3   [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]    [[[2, 5], [3, 4]], [[3, 5], [4, 4]]]

Idéalement, j'aimerais faire tout le tri en même temps pour que ce soit le plus efficace possible.

2
jss367 21 avril 2020 à 18:04

2 réponses

Meilleure réponse

Triez d'abord chacune des listes internes, puis la liste des conteneurs:

import pandas as pd

l1 = [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
l2 = [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]
l3 = [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]
l4 = [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]
small_df = pd.DataFrame({'col': [l1, l2, l3, l4]})


def sort(lst):
    """Sort inner list and then sort container list"""
    return sorted(sorted(e) for e in lst)


small_df['sorted'] = small_df['col'].apply(sort)
print(small_df)

Sortie

                                    col                                sorted
0  [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]  [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1  [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]  [[[2, 7], [4, 3]], [[4, 5], [9, 2]]]
2  [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]  [[[3, 3], [4, 4]], [[4, 6], [6, 3]]]
3  [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]  [[[2, 5], [3, 4]], [[3, 5], [4, 4]]]
3
Dani Mesejo 21 avril 2020 à 15:06

Alternative avec explode et map

small_df['sorted_col'] = (small_df['col'].explode().map(sorted).sort_values()
                           .groupby(level=0).agg(list))
print(small_df)

                                    col                            sorted_col
0  [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]  [[[1, 2], [3, 5]], [[3, 2], [5, 2]]]
1  [[[9, 2], [4, 5]], [[4, 3], [2, 7]]]  [[[2, 7], [4, 3]], [[4, 5], [9, 2]]]
2  [[[3, 3], [4, 4]], [[6, 3], [4, 6]]]  [[[3, 3], [4, 4]], [[4, 6], [6, 3]]]
3  [[[4, 4], [3, 5]], [[2, 5], [3, 4]]]  [[[2, 5], [3, 4]], [[3, 5], [4, 4]]]
1
anky 21 avril 2020 à 15:13