对组内的数据进行排序 - Pandas Dataframe

时间:2016-10-11 02:12:50

标签: python sorting pandas

我有以下数据框:

      As  Comb     Mu(+)    Name      Zone     f´c
33  0.37    2   6.408225   Beam_13   Final    30.0
29  0.37    2   6.408225   Beam_13   Begin    30.0
31  0.94    2  16.408225   Beam_13   Middle   30.0
15  0.54    2   9.504839   Beam_7    Final    30.0
11  0.54    2   9.504839   Beam_7    Begin    30.0
13  1.12    2  19.504839   Beam_7    Middle   30.0

我需要按照Name之前的数据对数据进行排序,然后按照Zone对数据进行排序,如下面的预期输出所示:

      As  Comb     Mu(+)    Name      Zone     f´c
11  0.54    2   9.504839   Beam_7    Begin    30.0
13  1.12    2  19.504839   Beam_7    Middle   30.0
15  0.54    2   9.504839   Beam_7    Final    30.0
29  0.37    2   6.408225   Beam_13   Begin    30.0
31  0.94    2  16.408225   Beam_13   Middle   30.0
33  0.37    2   6.408225   Beam_13   Final    30.0

我可以按索引排序,但不能按Name组中的名称和区域排序。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

最干净的方法是将NameZone列转换为类别类型,指定类别和顺序。

from io import StringIO

data = """
As,Comb,Mu(+),Name,Zone,f´c
33,0.37,2,6.408225,Beam_13,Final,30.0
29,0.37,2,6.408225,Beam_13,Begin,30.0
31,0.94,2,16.408225,Beam_13,Middle,30.0
15,0.54,2,9.504839,Beam_7,Final,30.0
11,0.54,2,9.504839,Beam_7,Begin,30.0
13,1.12,2,19.504839,Beam_7,Middle,30.0
"""

df = pd.read_csv(StringIO(data))

# convert Name and Zone to ordinal/category type
df.Name = df.Name.astype('category', categories=["Beam_7", "Beam_13"], ordered=True)
df.Zone = df.Zone.astype('category', categories=["Begin", "Middle", "Final"], ordered=True)

df.sort_values(by=['Name', 'Zone'])

这是输出:

enter image description here

可以找到其他选项here