我有一个带有索引的pandas DataFrame,我想自然排序.Natsort似乎不起作用.在构建DataFrame之前对索引进行排序似乎没有帮助,因为我对DataFrame的操作似乎搞乱了进程中的排序.关于如何自然地采用指数的任何想法?
from natsort import natsorted
import pandas as pd
# An unsorted list of strings
a = ['0hr', '128hr', '72hr', '48hr', '96hr']
# Sorted incorrectly
b = sorted(a)
# Naturally Sorted
c = natsorted(a)
# Use a as the index for a DataFrame
df = pd.DataFrame(index=a)
# Sorted Incorrectly
df2 = df.sort()
# Natsort doesn't seem to work
df3 = natsorted(df)
print(a)
print(b)
print(c)
print(df.index)
print(df2.index)
print(df3.index)
Run Code Online (Sandbox Code Playgroud)