相关疑难解决方法(0)

自然地排序Pandas DataFrame

我有一个带有索引的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)

python sorting natsort python-2.7 pandas

15
推荐指数
3
解决办法
4428
查看次数

标签 统计

natsort ×1

pandas ×1

python ×1

python-2.7 ×1

sorting ×1