看过来
《pandas 教程》 持续更新中,提供建议、纠错、催更等加作者微信: gr99123(备注:pandas教程)和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。
(编码题)假设你有一个如下的 pandas DataFrame,要求按索引标签值对数据行进行排序。
import pandas as pd
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]
}
df = pd.DataFrame(data, index=['x', 'y', 'z'])
df
'''
A B C
x 1 4 7
y 2 5 8
z 3 6 9
'''
需求:
代码如下:
df.sort_index(ascending=False)
'''
A B C
z 3 6 9
y 2 5 8
x 1 4 7
'''
df.reindex(['x', 'z', 'y'])
'''
A B C
x 1 4 7
z 3 6 9
y 2 5 8
'''
查看相关链接中的知识。
(完)
更新时间:2025-03-07 08:27:39 标签:pandas python 顺序