对具有字符串和数字的DataFrame索引进行排序 [英] Sort DataFrame index that has a string and number
本文介绍了对具有字符串和数字的DataFrame索引进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的df
DataFrame索引如下:
My df
DataFrame index looks like this:
Com_Lag_01
Com_Lag_02
Com_Lag_03
Com_Lag_04
Com_Lag_05
Com_Lag_06
Com_Lag_07
Com_Lag_08
Com_Lag_09
Com_Lag_10
Com_Lag_101
Com_Lag_102
Com_Lag_103
...
Com_Lag_11
Com_Lag_111
Com_Lag_112
Com_Lag_113
Com_Lag_114
...
Com_Lag_12
Com_Lag_120
...
Com_Lag_13
Com_Lag_14
Com_Lag_15
我想对这个索引进行排序,以便数字从Com_Lag_1
到Com_Lag_120
.如果使用df.sort_index()
,我将获得与上述相同的结果.关于如何正确排序此索引的任何建议?
I want to sort this index so that the numbers go from Com_Lag_1
to Com_Lag_120
. If I use df.sort_index()
I will get the same thing as above. Any suggestion on how to sort this index properly?
推荐答案
人们可以通过对索引的编号版本进行排序来尝试类似的操作
One could try something like this, by performing a sort on a numbered version of the index
import pandas as pd
# Create a DataFrame example
df = pd.DataFrame(\
{'Year': [1991 ,2004 ,2001 ,2009 ,1997],\
'Age': [27 ,25 ,22 ,34 ,31],\
},\
index = ['Com_Lag_1' ,'Com_Lag_12' ,'Com_Lag_3' ,'Com_Lag_24' ,'Com_Lag_5'])
# Add of a column containing a numbered version of the index
df['indexNumber'] = [int(i.split('_')[-1]) for i in df.index]
# Perform sort of the rows
df.sort(['indexNumber'], ascending = [True], inplace = True)
# Deletion of the added column
df.drop('indexNumber', 1, inplace = True)
编辑2017-V1 :
为避免SettingWithCopyWarning:
To avoid SettingWithCopyWarning:
df = df.assign(indexNumber=[int(i.split('_')[-1]) for i in df.index])
编辑2017-V2 for Pandas版本0.21.0
import pandas as pd
print(pd.__version__)
# Create a DataFrame example
df = pd.DataFrame(\
{'Year': [1991 ,2004 ,2001 ,2009 ,1997],\
'Age': [27 ,25 ,22 ,34 ,31],\
},\
index = ['Com_Lag_1' ,'Com_Lag_12' ,'Com_Lag_3' ,'Com_Lag_24' ,'Com_Lag_5'])
df.reindex(index=df.index.to_series().str.rsplit('_').str[-1].astype(int).sort_values().index)
这篇关于对具有字符串和数字的DataFrame索引进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文