Pandas多索引DataFrame排序



只显示我的数据

In [14]: new_df
Out[14]: 
action_type                           1     2    3
user_id                                           
0000110e00f7c85f550b329dc3d76210   31.0   4.0  0.0
00004931fe12d6f678f67e375b3806e3    8.0   4.0  0.0
0000c2b8660766ed74bafd48599255f0    0.0   2.0  0.0
0000d8d4ea411b05e0392be855fe9756   19.0   0.0  3.0
ffff18540a9567b455bd5645873e56d5    1.0   0.0  0.0
ffff3c8cf716efa3ae6d3ecfedb2270b   58.0   2.0  0.0
ffffa5fe57d2ef322061513bf60362ff    0.0   2.0  0.0
ffffce218e2b4af7729a4737b8702950    1.0   0.0  0.0
ffffd17a96348904fe49216ba3c7006f    1.0   0.0  0.0
[9 rows x 3 columns]
In [15]: new_df.columns
Out[15]: Int64Index([1, 2, 3], dtype='int64', name=u'action_type')
In [16]: new_df.index
Out[16]: 
Index([u'0000110e00f7c85f550b329dc3d76210',
       u'00004931fe12d6f678f67e375b3806e3',
       ...
       u'ffffa5fe57d2ef322061513bf60362ff',
       u'ffffce218e2b4af7729a4737b8702950',
       u'ffffd17a96348904fe49216ba3c7006f'],
      dtype='object', name=u'user_id', length=9)

我想要的输出是:

# sort by the action_type value 1
action_type                           1     2    3
user_id
ffff3c8cf716efa3ae6d3ecfedb2270b   58.0   2.0  0.0                                         
0000110e00f7c85f550b329dc3d76210   31.0   4.0  0.0
0000d8d4ea411b05e0392be855fe9756   19.0   0.0  3.0
00004931fe12d6f678f67e375b3806e3    8.0   4.0  0.0
ffff18540a9567b455bd5645873e56d5    1.0   0.0  0.0
ffffce218e2b4af7729a4737b8702950    1.0   0.0  0.0
ffffd17a96348904fe49216ba3c7006f    1.0   0.0  0.0
0000c2b8660766ed74bafd48599255f0    0.0   2.0  0.0
ffffa5fe57d2ef322061513bf60362ff    0.0   2.0  0.0
[9 rows x 3 columns]
# sort by the action_type value 2
action_type                           1     2    3
user_id
00004931fe12d6f678f67e375b3806e3    8.0   4.0  0.0
0000110e00f7c85f550b329dc3d76210   31.0   4.0  0.0
ffff3c8cf716efa3ae6d3ecfedb2270b   58.0   2.0  0.0                                         
0000c2b8660766ed74bafd48599255f0    0.0   2.0  0.0
ffffa5fe57d2ef322061513bf60362ff    0.0   2.0  0.0
0000d8d4ea411b05e0392be855fe9756   19.0   0.0  3.0
ffff18540a9567b455bd5645873e56d5    1.0   0.0  0.0
ffffce218e2b4af7729a4737b8702950    1.0   0.0  0.0
ffffd17a96348904fe49216ba3c7006f    1.0   0.0  0.0
[9 rows x 3 columns]

所以,我想做的是用action_typeDataFrame进行排序,即1, 2, 3或它们中任何一个的和(action_type 1+2, 1+3, 2+3, 1+2+3的和)

输出应该按照每个用户的action_type的值(1, 2 or 3)或每个用户的action _type的总和(例如action _type 1和action _type 2的总和,以及任何组合,例如action _type1和action_type 3的总和、action _type2和action _type3的总和,action _type1与action _type2的总和以及action _type 3)进行排序。

例如:

对于用户id 0000110e00f7c85f550b329dc3d76210,action_type 1的值为31.0,action_type2的值为4,action_type3的值为3。该用户的action_type 1和action_type 2之和为31.0+4.0=35.0

我试过new_df.sortlevel(),但它似乎只是通过user_id而不是通过action_type(1, 2, 3) 处理了数据帧

我该怎么做,谢谢!

更新:

如果你想按列排序,只需尝试sort_values

df.sort_values(column_names)

示例:

In [173]: df
Out[173]:
   1  2  3
0  6  3  8
1  0  8  0
2  3  8  0
3  5  2  7
4  1  2  1

按列2 降序排序

In [174]: df.sort_values(by=2, ascending=False)
Out[174]:
   1  2  3
1  0  8  0
2  3  8  0
0  6  3  8
3  5  2  7
4  1  2  1

按列和降序排列2+3

In [177]: df.assign(sum=df.loc[:,[2,3]].sum(axis=1)).sort_values('sum', ascending=False)
Out[177]:
   1  2  3  sum
0  6  3  8   11
3  5  2  7    9
1  0  8  0    8
2  3  8  0    8
4  1  2  1    3

旧答案:

如果我说得对,你可以这样做:

In [107]: df
Out[107]:
   a  b  c
0  9  1  4
1  0  5  7
2  5  9  8
3  3  9  7
4  1  2  5
In [108]: df.assign(sum=df.sum(axis=1)).sort_values('sum', ascending=True)
Out[108]:
   a  b  c  sum
4  1  2  5    8
1  0  5  7   12
0  9  1  4   14
3  3  9  7   19
2  5  9  8   22

最新更新