Python 熊猫中的 sort_values() 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42477572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sort_values() method in pandas
提问by Gilbert
I have the following subset of data and I need to sort the Education
column in ascending order; from 0 to 17
.
我有以下数据子集,我需要Education
按升序对列进行排序;从0 to 17
.
I tried the following code without success.
我尝试了以下代码但没有成功。
suicide_data.sort_index(axis=0, kind='mergesort')
also...
还...
suicide_data.Education.sort_values()
and...
和...
suicide_data.sort_values('Education')
Here is the error I'm getting...
这是我得到的错误...
TypeError: '>' not supported between instances of 'float' and 'str'
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?
文档说str
可以使用该sort_values()
方法进行排序。有谁知道如何Education
按升序对列进行排序?
回答by miradulo
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 tothe floats in your column. Assuming you want to sort the values numerically, you could convert them to integer type and thensort. 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
.
看起来Education
您的 DataFrame 列中必须有混合类型。错误消息告诉您它无法将字符串与列中的浮点数进行比较。假设您想按数字对值进行排序,则可以将它们转换为整数类型,然后进行排序。我建议您无论如何都这样做,因为混合类型对于您的 DataFrame 中的任何操作都没有太大用处。然后使用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
将按您不想要的索引对您的 DataFrame 进行排序,以及您的第二次尝试
suicide_data.Education.sort_values()
would only return the sorted Series - they are completely invalid approaches.
只会返回排序的系列 - 它们是完全无效的方法。
回答by Jyothsna Harithsa
suicide_data['Education'].sort_values('Education', ascending = 'True')