1

Consider the following dataset:

seq_id  a  b  id 

1       20 13 1
2       21 14 1
3       18 19 1
1       22 15 2
2       21 14 2
1       25 12 1
2       27 13 1

I need to sort my data set by id , however I have more than one group for the specific id. for example there are 2 groups of id 1. I need the following result:

seq_id  a  b  id 

1       20 13  1
2       21 14  1
3       18 19  1
1       25 12  1
2       27 13  1
1       22 15  2
2       21 14  2

I tried this script which is not correct:

sort = lambda x: sorted(x,reverse=False)
data = data.sort_values(by=['id'],ascending=True)
data.groupby('id').seq_id.apply(sort)

thanks for your comments.

Or is there any way to change the second group's name of id = 1 to separate them from each other. for example lets say, for the first group id = 1 and for the second group id = 1-1

1 Answer 1

1

Try:

data = data.sort_values(by=['id'], ascending=True, kind='mergesort')

Adding kind='mergesort' switches the sort_values call to use a "stable" sorting algorithm (see documentation), which means that the original order of rows will be preserved for rows that have an equal value for the sorting criterion.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.