0

Am trying to create a groups from the array by target index

for example this my Array

Am trying to target index [1] of each child of the data array

data = [('0', 'COL1', 'date'),('1', 'COL2', 'date'), ('2', 'COL1', 'date'), ('3', 'COL3', 'date') , ('4', 'COL1', 'date'),('6', 'COL2', 'date')]

expected output is

[[('0', 'COL1', 'date'),('2', 'COL1', 'date'),('4', 'COL1', 'date')],[('1', 'COL2', 'date'),('6', 'COL2', 'date')],[('3', 'COL3', 'date')]]

Thank you any help will appreciated!

3
  • 1
    itertools.groupby? Commented Sep 21, 2019 at 13:14
  • @JonClements can you give close example. Commented Sep 21, 2019 at 13:18
  • 1
    Trying to find you a duplicate that explains it but it comes up a lot of times: you basically sort it by the key and materialise the grouped items as a list, then group it by the key, eg: [list(g) for k, g in groupby(sorted(data, key=lambda L: L[1]), lambda L: L[1])] Commented Sep 21, 2019 at 13:19

1 Answer 1

1
import numpy as np
data = np.array(data)
data[data[:,1].argsort()]
Sign up to request clarification or add additional context in comments.

3 Comments

am having issue printing out data[data[:,1].argsort()] but its ok, this line is enough np.array(data)
What is the issue you are facing with printing?
That will give numpy array, incase you like you can convert to list: data[data[:,1].argsort()].tolist()

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.