Pandas将索引排序为整数

Pra*_*n M 8 python pandas

我有一个由CSV构建的数据框.当我使用df.to_html()它将它转换为html时,它给我一个与CSV文件不同的排序顺序.

当我在控制台上打印数据框时,它给出了以下输出

reasons_for_make_good 0 None 1 None 10 None 11 None 12 None 13 None 14 None 15 None 16 None 17 None 18 None 19 None 2 None 3 None 4 None 5 None 6 None 7 None 8 None 9 None

为什么索引按字符串排序,是否可以将其排序为整数?

谷歌搜索答案但无济于事.

jez*_*ael 10

尝试将index转换为intby Index.astype然后sort_index:

df.index = df.index.astype(int)
df = df.sort_index()
Run Code Online (Sandbox Code Playgroud)