Gilbert Gilbert -4 years ago 517
Python Question

sort_values() method in pandas

I have the following subset of data and I need to sort the

column in ascending order; from
0 to 17

enter image description here

I tried the following code without success.

suicide_data.sort_index(axis=0, kind='mergesort')





Here is the error I'm getting...

TypeError: '>' not supported between instances of 'float' and 'str'

The documentation says that
can be sort with the
method. Does anyone know how to sort the
column in ascending order?

Answer Source

It looks like you must have mixed types within the Education column of your DataFrame. The error message is telling you that it cannot compare the strings to the floats in your column. Assuming you want to sort the values numerically, you could convert them to integer type and then sort. I'd advise you do this anyways, as mixed types won't be too useful for any operations in your DataFrame. Then use DataFrame.sort_values.

suicide_data['Education'] = suicide_data['Education'].astype('int')

It is also worth pointing out that your first attempt,

suicide_data.sort_index(axis=0, kind='mergesort')

would sort your DataFrame by the index, which you don't want, and your second attempt


would only return the sorted Series - they are completely invalid approaches.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download