Home > Blockchain >  pandas: How to sort values in order of most repeated to least repeated?
pandas: How to sort values in order of most repeated to least repeated?

Time:05-17

Suppose a df as:

   A   B  ...
   2   .
   3   .
   2   .
   3   
   2   
   1

I expect output to be:

   A   B  ...
   2   .
   2   .
   2   .   
   3   
   3   
   1

Because 3 was repeated more, then 2 and so on.

CodePudding user response:

This would work

df['Frequency'] = df.groupby('A')['A'].transform('count')
df.sort_values('Frequency', inplace=True, ascending=False)

CodePudding user response:

First add a new column counting the repetitions:

>>> df['C'] = df.groupby('A')['A'].transform('count')

Then sort by this new column:

>>> df.sort_values('C', ascending=False)

CodePudding user response:

Try value_counts and argsort

out = df.iloc[(-df.A.value_counts().reindex(df.A)).argsort()]
Out[647]: 
   A     B  ...
0  2     .  NaN
2  2     .  NaN
4  2  None  NaN
1  3     .  NaN
3  3  None  NaN
5  1  None  NaN

CodePudding user response:

This works:

# Suppose you have a df like this:
import pandas as pd
df = pd.DataFrame({'A':[2,3,2,3,2,1], 'B':range(6)})
   A  B
0  2  0
1  3  1
2  2  2
3  3  3
4  2  4
5  1  5

# you can pass a sorting function to sort_values as key:
df = df.sort_values(by='A', key=lambda x: x.map(x.value_counts()), ascending=False)
   A  B
0  2  0
2  2  2
4  2  4
1  3  1
3  3  3
5  1  5
  • Related