pandas 中的sort_values()方法 [英] sort_values() method in pandas

查看:130
本文介绍了 pandas 中的sort_values()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据子集,我需要按升序对Education列进行排序;来自0 to 17.

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 following code without success.

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

还...

suicide_data.Education.sort_values()

和...

suicide_data.sort_values('Education')

这是我遇到的错误...

Here is the error I'm getting...

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

文档说str可以用sort_values()方法排序.有人知道如何按升序对Education列进行排序吗?

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

推荐答案

似乎您的DataFrame的Education列中必须具有混合类型.错误消息告诉您,它无法将字符串列中的浮点数进行比较.假设要对数值进行数字排序,可以将它们转换为整数类型,然后 then 进行排序.我建议您还是这样做,因为混合类型对于DataFrame中的任何操作都不会太有用.然后使用 DataFrame.sort_values .

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')

将按不需要的索引和第二次尝试对您的DataFrame进行排序

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

suicide_data.Education.sort_values()

只会返回排序后的Series-它们完全是无效的方法.

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

这篇关于 pandas 中的sort_values()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆