user4991878
user4991878

Reputation:

how to sort a panda dataframe in order shwoing redundant values in a column first?

I have a panda dataframe which has a column "test_id"

test_id
      2
      5
      1
      3
      3
      3
      4
      4
      4
      2
      9

I want to sort the dataframe such that this column is:

test_id
      3
      3
      3
      4
      4
      4
      2
      2
      1
      5
      9

Please help. Thanks!

Upvotes: 0

Views: 29

Answers (1)

zipa
zipa

Reputation: 27869

Here is one example of how you can do it, using a helper column that you can delete afterwards:

import pandas as pd

df = pd.DataFrame({'test_id': [2, 5, 1, 3, 3, 3, 4, 4, 2, 9],
                   'other_column': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]})


df['tbd'] = df.groupby(['test_id']).transform('count')

df.sort_values(['tbd', 'test_id'], inplace=True, ascending=(False, True))

del df['tbd']

df

   other_column  test_id
3             4        3
4             5        3
5             6        3
0             1        2
8             9        2
6             7        4
7             8        4
2             3        1
1             2        5
9            10        9

Upvotes: 1

Related Questions