ちょうど私のデータを表示パンダマルチインデックス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_type
でDataFrame
を並べ替えることで、それがあります1, 2, 3
またはそれらのいずれかの合計(action_type
の合計が1+2, 1+3, 2+3, 1+2+3
)
出力はaction_typeの値でソートする必要がありますaction_type 1とaction_type 2の合計、action_type 1とaction_type 3の合計、action_type 2とaction_type 3の合計、action_typeの合計(たとえば、action_type 1とaction_type 3の合計)などのaction_typeの合計1およびaction_type 2およびaction_type 3)を含む。例えば
:ユーザID 0000110e00f7c85f550b329dc3d76210
ため
は、ACTION_TYPE 1の値が31.0であり、ACTION_TYPE 2の値は4であり、ACTION_TYPE 3の値がACTION_TYPE 1及びACTION_TYPE 2の合計3です。このユーザーの私はnew_df.sortlevel()
を試してみました35.0
= 31.0 + 4.0であるが、それだけでuser_id
でデータフレームをsoredたようで、action_type(1, 2, 3)
で、私はそれを行うことができませんどのように、あなたに感謝!
心所望の出力を投稿すると、あなたが '1 + 2、1 + 3、2 + 3、1 + 2 + 3'の合計で何を意味するのかを説明してください。 – Deusdeorum
@HugoHonorem、こんにちは。 –
'action_type(1、2、3)'とは何ですか? – MaxU