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)
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.
I'm trying to use
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)
but that gives an error message:
Traceback (most recent call last):
File "", line 1, in
df.sort_index(key=float, axis=1)
TypeError: sort_index() got an unexpected keyword argument 'key'
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!!
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'))
# 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
I hope this helps
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 (...
5.
使用
.
sort_values
() 查看按照值排序的数据
5.1.
sort_values
() 语法
语法:.
sort_values
(by, axis=0, ascending = Ture, inplace = Flase, kind = ‘quick
sort
’, na_position=‘last’, ignore_index=False,
key
=None)
相对于 .
sort
_index()函数,此处多了一个 by
Python
内建的list.
sort
()方法和
sort
ed()函数都可以实现对列表进行排序。
一、list.
sort
()方法:list.
sort
(
key
=function, reverse=Boolean)
list.
sort
()方法是对列表list直接进行排序,排序完成后原来的list列表
中
的元素位置变化,按排序顺序排列。
可选的关键字参数reverse为布尔型数据,设置排序方向,默认值是False,按照升序排序。当reverse值可为True...
pandas是
python
环境下最有名的数据统计包,而DataFrame翻译为数据框,是一种数据组织方式,这么说你可能无法从感性上认识它,举个例子,你大概用过Excel,而它也是一种数据组织和呈现的方式,简单说就是表格,而在在pandas
中
用DataFrame组织数据,如果你不print DataFrame,你看不到这些数据。
首先,是想用pandas操作“.csv"文件,当然有很多操作,用di...
在学习《利用
Python
进行数据分析》一书时出现
TypeError:
sort
_index() got an unexpected
key
word argument ‘by’
问题原因分析:pandas模块更新了,对有些方法进行重写,参数发生改变。
解决方法:将
sort
_index()改为
sort_values
()即可。
`df.
sort_values
()` 是一个 Pandas DataFrame 对象的方法,用于按照一个或多个列的值对数据进行排序。它返回一个新的 DataFrame 对象,而不是修改原来的 DataFrame。
语法如下:
```
python
df.
sort_values
(by, axis=0, ascending=True, inplace=False, ignore_index=False,
key
=None)
参数解释:
- `by`:指定排序的列名或列名列表。
- `axis`:指定排序轴的方向。0表示按行排序,1表示按列排序。
- `ascending`:指定是否升序排列,默认为True。
- `inplace`:是否在原 DataFrame 上进行排序,默认为False(即返回一个新的 DataFrame)。
- `ignore_index`:是否忽略排序后的索引,并返回一个新的索引,默认为False。
- `
key
`:指定一个函数用于排序,而不是默认的按值排序。
```
python
import pandas as pd
data = {'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emily'],
'age': [25, 18, 32, 45, 29],
'score': [80, 95, 70, 60, 85]}
df = pd.DataFrame(data)
# 按照 age 列升序排列
df_
sort
ed = df.
sort_values
(by='age', ascending=True)
print(df_
sort
ed)
输出结果为:
name age score
1 Bob 18 95
0 Alice 25 80
4 Emily 29 85
2 Charlie 32 70
3 David 45 60