0

I am quite new to pandas (working with 3rd party code, forced to use it!), and I have a dataframe which looks like so:

name_id    cookie_id    file_name_id
John       56           /some/loc
Doe        45           /some/loc2
John       67           /some/loc3
hilary     768          /some/loc4
wendy      8            /some/loc3
hilary     4            /some/loc4

I would like to sort them by the name_id like so:

name_id    cookie_id    file_name_id
Doe        45           /some/loc2
John       56           /some/loc
John       67           /some/loc3
hilary     768          /some/loc4
hilary     4            /some/loc4
wendy      8            /some/loc3

I am looking at:

df.sort_values(by=['name_id'])

and it does seem to give me the correct answer, but since I am new to pandas, I am afraid there might be some gotchas I need to aware of.

AJW
  • 5,569
  • 10
  • 44
  • 57
  • Does this answer your question? [how to sort pandas dataframe from one column](https://stackoverflow.com/questions/37787698/how-to-sort-pandas-dataframe-from-one-column) – tianlinhe May 15 '20 at 10:17

1 Answers1

1

df.sort_values(by=['name_id']) should be perfectly fine to use. Watch out for spaces in the beginning of the name_id string as those would be sorted first. For example, " wendy" would be placed in the top in your case.

CHRD
  • 1,917
  • 1
  • 15
  • 38