2

I'm trying to sort a datafarame using the pandas sort_values() function but the returned result is sorted in a strange way as shown in the image attached enter image description here

3
  • it looks like your Citations column is of type string. Convert it to number then sorting should work Commented Nov 5, 2019 at 8:00
  • I checked the au_df_srtd.dtypes' and it returns dtype: object` for all columns Commented Nov 5, 2019 at 8:02
  • @E.Aly Ok check mine or jezraels answer Commented Nov 5, 2019 at 8:02

2 Answers 2

3

Problem is column Citation is strings repr of numbers, so need convert to numeric by Series.astype:

au_df['Citation'] = au_df['Citation'].astype(int)
au_df_srtd = au_df.sort_values('Citations')

If not working because at least one string value in numbers use to_numeric with errors='coerce' for convert non numbers to NaNs:

au_df['Citation'] = pd.to_numeric(au_df['Citation'], errors='coerce')
au_df_srtd = au_df.sort_values('Citations')
Sign up to request clarification or add additional context in comments.

2 Comments

You're faster 7 sec
nice one with to_numeric tho.
2

Because they must be strings, so try using astype:

au_df['Citations'] = au_df['Citations'].astype(int)
au_df_srtd = au_df.sort_values('Citations')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.