Soufiane Sabiri
Soufiane Sabiri

Reputation: 768

Order column by number of commas in Pandas DataFrame

I have a DataFrame like this:

>>> df = pd.DataFrame({'id_police':['s123','s124','s125','s126','s127'],
                   'raison':['dog','cat','cow','dog, cat, cow','dog, cat'],
    })
>>> df
  id_police         raison
0      s123            dog
1      s124            cat
2      s125            cow
3      s126  dog, cat, cow
4      s127       dog, cat

And I want to order by column raison in a DESC fashion like this (and ID is reset but id_police is preserved):

  id_police         raison
0      s126  dog, cat, cow
1      s127       dog, cat
2      s123            dog
3      s124            cat
4      s125            cow

Please help, I think I need a lambda function for this...

Upvotes: 0

Views: 66

Answers (2)

Erfan
Erfan

Reputation: 42916

You can use sort_values for this and before that creating a count column for ,:

df['count'] = df.raison.str.count(',')
df = df.sort_values('count', ascending=False).reset_index(drop=True)

df = df.drop('count', axis=1)

print(df)
  id_police         raison
0      s126  dog, cat, cow
1      s127       dog, cat
2      s123            dog
3      s124            cat
4      s125            cow

Upvotes: 0

BENY
BENY

Reputation: 323316

You can using count + argsort

df.iloc[(-df.raison.str.count(',')).argsort()]
Out[12]: 
  id_police         raison
3      s126  dog, cat, cow
4      s127       dog, cat
0      s123            dog
1      s124            cat
2      s125            cow

Upvotes: 4

Related Questions