Search code examples
pythonpandassortingseries

Sort series on index for the values that are equal


I have a Pandas series here:

s1=pd.Series([23,23,21,21,25,25,25])

Now when i do s1.value_counts(), i get another series which is this image:

enter image description here

What i want to do is, get the indexes with duplicate values, sort them based on index and place them back to the series. In this case, the output would be:

enter image description here

I have tried putting it in a dictionary, flipping the values and then sorting the values. But is there a much more pandas efficient way to do this? Thank you in advance!


Solution

  • Is this is what you are after?:

    import pandas as pd
    s1=pd.Series([23,23,21,21,25,25,25])
    s1.value_counts().sort_index(ascending=True).sort_values(ascending=False)