sort_values() method in pandas

后端 未结 2 1307
野性不改
野性不改 2021-01-01 02:43

I have the following subset of data and I need to sort the Education column in ascending order; from 0 to 17.

I tried the followin

相关标签:
2条回答
  • 2021-01-01 03:13
    suicide_data['Education'].sort_values('Education', ascending = 'True')
    
    0 讨论(0)
  • 2021-01-01 03:16

    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')
    suicide_data.sort_values(by='Education')
    

    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

    suicide_data.Education.sort_values()
    

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

    0 讨论(0)
提交回复
热议问题