Custom sort value_counts() by index

Hi all,

I’d like to know whether someone knows whether it is possible to have a custom sort on the index of value_counts.

Currently I get this:

afbeelding

But i’d like to change it somehow to this:

afbeelding

Is it possible?

1 Like

Hi Léon,

Try this:

combined_up['test'].value_counts(dropna=False)[['<1', '1-2', '3-4', '5-6', '7-8', '>10', np.nan]]
1 Like

If that index was veeeeeery long and rearranging it would take a lot of time: I’d change it to a dataframe, and work from there:

df['col'].value_counts().to_frame()
1 Like

Thanks @Elena_Kosourova and @adam.kubalica.

Just curious @adam.kubalica, once you have it in a dataframe, how would a custom sort of lets say 50 unique values look like?

1 Like

I think I would start with some regex magic to extract only the first number from each row:
so we’d turn this:
<1
1-2
3-4
into this:
1
1
3
I guess we could deduct 0.1 from strings with ‘<’ in them so the output would be:
0.9
1
3
shouldn’t be that hard to figure it out, but it only makes sense in big amount of those different values

1 Like

Interesting, thanks for sharing!

# create a df:
vals = ['<1', '1-2', '3-4', '5-12', '12-19', '>20', np.nan]
numbers = [12,323,5,644,33,5,6]
df = pd.DataFrame({'Range':vals,'value_count':numbers})

# extract first number
df['counts'] = df['Range'].str.extract('(\d+)')

# make the '<1' smaller so it's not the same as '1': 
df.loc[~(df['Range'].str.find("<") == -1) & (df['Range'].notnull()), 'counts'] = 0.9
df