Python 多索引数据帧排序

Python 多索引数据帧排序,python,pandas,dataframe,Python,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 0000c2b8660

只需显示我的数据

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]
所以,我想做的是按照
动作类型对
数据帧进行排序,也就是说
1,2,3
或它们的总和(
action\u类型
1+2,1+3,2+3,1+2+3

输出应按每个用户的操作类型值(
1、2或3
)或操作类型总和排序(例如动作类型1和动作类型2之和,以及任何组合,例如动作类型1和动作类型3之和,动作类型2和动作类型3之和,动作类型1和动作类型2和动作类型3之和)每个用户的

例如:

对于用户id
00001100E00F7C85F550B329DC3D76210
,动作类型1的值为31.0,动作类型2的值为4,动作类型3的值为3。此用户的动作类型1和动作类型2之和为31.0+4.0=35.0

我尝试了
new\u df.sortlevel()
,但它似乎只是按照
用户id
对数据帧排序,而不是按照
操作类型(1,2,3)


我该怎么做,谢谢!

更新:

如果你想按列排序,试试看

例如:

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

介意发布所需输出并解释一下你所说的
1+2,1+3,2+3,1+2+3
@HugoHonorem,你好,我已经发布了我所需的输出。什么是
动作类型(1,2,3)
?仍然有点不清楚,仅前4行的预期输出是什么?@HugoHonorem,我想对数据帧进行排序。按照action_type值排序的数据帧有3个action_type,1、2和3。我想按照action_type 1或2或3的值排序数据帧。或者按照action_type 1和action_ty的值之和排序数据帧pe 2,或action_type 1和action_type 3的值之和,或action_type 1和action_type 3的值之和,或action_type 1和action_type 2和action_type 3的值之和。用户ID是唯一的。很抱歉没有给出正确的数据输出。您的答案是正确的!谢谢。@AlexanderYau,非常欢迎您!它是在发布所需输出数据集的示例之前,有点难以理解您想要实现什么
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