Exemple I have a list:
[[0,'A',50.1],
[1,'B',50.0],
[2,'C',50.2],
[3,'D',50.7],
[4,'E',50.3]]
I want to order it based on the 3rd elemnts only so I get a result like this
[[1,'B',50.0],
[0,'A',50.1],
[2,'C',50.2],
[4,'E',50.3],
[3,'D',50.7]]
and then reorder the index so the Final result would be
Final = [[0,'B',50.0],
[1,'A',50.1],
[2,'C',50.2],
[3,'E',50.3],
[4,'D',50.7]]
and then I have the indexes in some grouping
G = [[0,1],[1,3][2,3,4]]
I want based on G as indexes of Final have the Grouping like this
[['B','A'],['A','E']['C','E','D']]
Can u help me out
CodePudding user response:
Use sorted
by third element first with append first value of nested lists with enumerate
:
L = [[i, *x[1:]] for i, x in enumerate(sorted(L, key=lambda x: x[2]))]
print (L)
[[0, 'B', 50.0], [1, 'A', 50.1], [2, 'C', 50.2], [3, 'E', 50.3], [4, 'D', 50.7]]
Last filter matched values by position of list (because same like first value of nested list):
out = [[L[y][1] for y in x] for x in G]
print (out)
[['B', 'A'], ['A', 'E'], ['C', 'E', 'D']]
CodePudding user response:
I actually got the same question as jezrael did, but have not enough reputation to add such a comment.
As jezrael gave a solution not using pandas, I'm going to gave one using pandas just in case others find the same question in the future with larger dataset and wish to use pandas.
In pd.DataFrame
, it's not necessary to set an index by yourself. The first column therefore is actually unnecessary, you may delete it. So here is the solution:
foo = (foo.sort_values(by=[2], ascending=True)
.drop(columns=[0]) # if you delete the index column, don't do this
.reset_index(drop=True)
.reset_index(drop=False)
)
After then, you may use to_dict()
method for mapping, or to_numpy()
to adopt the approach jezrael indicated.
Here's the explanation, the first line foo.sort_values(by=['value'], ascending=True)
is to order the dataframe by the ascending.
foo = foo.sort_values(by=[3], ascending=True)
print(foo)
[Out]:
0 1 2
1 1 B 50.0
0 0 A 50.1
2 2 C 50.2
4 4 E 50.3
3 3 D 50.7
Then I dropped the first column.
foo = (...
.drop(columns=[0])
)
print(foo)
[Out]:
1 2
1 B 50.0
0 A 50.1
2 C 50.2
4 E 50.3
3 D 50.7
You may see that the index is not in an ascending order, we need to reset it first and then assign:
foo = (...
.reset_index(drop=True) # this one reset the index
.reset_index(drop=False) # this one assign the dropped index
# ... with the col-name 'index'
)
print(foo)
[out]:
index 1 2
0 0 B 50.0
1 1 A 50.1
2 2 C 50.2
3 3 E 50.3
4 4 D 50.7