当有如下DataFrame时:
import pandas as pd
df = pd.DataFrame([1, 1, 1, 1, 1], index=[100, 29, 234, 1, 150], columns=['A'])
如何在索引和列值的每个组合完好无损的情况下按索引对该数据帧进行排序?
最佳答案
Dataframes 有一个 sort_index
方法默认返回一个副本。通过 inplace=True
就地操作。
import pandas as pd
df = pd.DataFrame([1, 2, 3, 4, 5], index=[100, 29, 234, 1, 150], columns=['A'])
df.sort_index(inplace=True)
print(df.to_string())
给我:
A
1 4
29 2
100 1
150 5
234 3
关于python - 如何按索引对 Pandas DataFrame 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211737/