1

In a datframe such as:

Employee      list_of_children              child

Jonathen      ['John', 'Bob','Jennifer']    John

Jonathen      ['John', 'Bob','Jennifer']    Bob

Jonathen      ['John', 'Bob','Jennifer']    Jennifer

Emily         ['Clark']                     Clark

Hans          ['Watson', 'Hans Jr']         Watson

Hans          ['Watson', 'Hans Jr']         Watson

How do I sort the above dataframe so that the employee with least children is first?

Employee      list_of_children              child

Emily         ['Clark']                     Clark

Hans          ['Watson', 'Monica']         Watson

Hans          ['Watson', 'Monica']         Monica

Jonathen      ['John', 'Bob','Jennifer']    John

Jonathen      ['John', 'Bob','Jennifer']    Bob

Jonathen      ['John', 'Bob','Jennifer']    Jennifer
Mohamad Moustafa
  • 479
  • 5
  • 19

1 Answers1

2

We can do with argsort after get the len

df = df.iloc[df['list_of_children'].str.len().argsort()]
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Why are you turning list to string? ```["hello", "world"]``` is shorter than ```["HELLLLLLLOOOOOOOOOOOO"]``` – Mohamad Moustafa Jul 19 '19 at 14:26
  • @MohamadMoustafa this is not turn list to str , this is count the len of list , ["hello", "world"] return 2 ["HELLLLLLLOOOOOOOOOOOO"] return 1 – BENY Jul 19 '19 at 14:27