在Python中使用key的sort_values() [英] sort_values() with key in Python

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

问题描述

我有一个数据框,其中列名是时间(0:00、0:10、0:20,...,23:50).现在,它们以字符串顺序排序(所以0:00是第一个,最后9:50是最后一个),但是我想在时间之后对其进行排序(所以0:00是第一个,而23:50是最后一个).

I have a dataframe where the column names are times (0:00, 0:10, 0:20, ..., 23:50). Right now, they're sorted in a string order (so 0:00 is first and 9:50 is last) but I want to sort them after time (so 0:00 is first and 23:50 is last).

如果时间是一列,则可以使用

If time is a column, you can use

df = df.sort(columns='Time',key=float)

但是1)仅当 time 本身是列而不是列名时才有效,并且2)不推荐使用sort(),因此我尝试放弃使用它.

But 1) that only works if time is a column itself, rather than the column names, and 2) sort() is deprecated so I try to abstain from using it.

我正在尝试使用

df = df.sort_index(axis = 1)

,但是由于列名是字符串格式,所以它们会根据字符串键进行排序.我已经尝试过

but since the column names are in string format, they get sorted according to a string key. I've tried

df = df.sort_index(key=float, axis=1) 

但是会出现错误消息:

Traceback (most recent call last):
  File "<ipython-input-112-5663f277da66>", line 1, in <module>
      df.sort_index(key=float, axis=1)
TypeError: sort_index() got an unexpected keyword argument 'key'

有人对如何解决这个问题有想法吗?因此,烦恼的是sort_index()和sort_values()-没有关键参数!

Does anyone have ideas for how to fix this? So annoying that sort_index() - and sort_values() for that matter - don't have the key argument!!

推荐答案

尝试使用sorted内置函数对列进行排序,并将输出传递到数据帧以进行索引.以下应作为工作示例:

Try sorting the columns with the sorted builtin function and passing the output to the dataframe for indexing. The following should serve as a working example:

import pandas as pd


records = [(2, 33, 23, 45), (3, 4, 2, 4), (4, 5, 7, 19), (4, 6, 71, 2)]
df = pd.DataFrame.from_records(records, columns = ('0:00', '23:40', '12:30', '11:23'))
df
#    0:00  23:40  12:30  11:23
# 0     2     33     23     45
# 1     3      4      2      4
# 2     4      5      7     19
# 3     4      6     71      2

df[sorted(df,key=pd.to_datetime)]

#    0:00  11:23  12:30  23:40
# 0     2     45     23     33
# 1     3      4      2      4
# 2     4     19      7      5
# 3     4      2     71      6

我希望这对您有帮助

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

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