python - Pandas: sort dataframe on the basis of number of rows for the column value -


i have dataframe this:

a    b 1    2 3    2 2    3 6    3 7    3 5    4 

i want sort dataframe on basis of number of rows values of b output :

a    b 2    3 6    3 7    3 1    2 3    2 5    4 

any possible 1 liner ?

you can sort temporary column (actually, dataframe single column, since sorting series can cause stable-ness problem) created based on value counts, , index original dataframe on result:

print df.loc[df[['b']].replace(df.b.value_counts().to_dict()).sort('b', ascending=false).index] 

output:

    b 2  2  3 3  6  3 4  7  3 0  1  2 1  3  2 5  5  4 

Comments