我有以下数据框:
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
组中的名称和区域排序。有什么想法吗?
答案 0 :(得分:3)
最干净的方法是将Name
和Zone
列转换为类别类型,指定类别和顺序。
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'])
这是输出:
可以找到其他选项here