熊猫DataFrame Groupby排序问题

时间:2018-07-06 15:25:55

标签: python pandas

我有这种形式的数据框。当我通过不需要的自动排序应用分组时,有没有办法做到这一点。

    import pandas as PD

d = {"ABC":["1.iab units (desktop + mobile) + non-expanding adhesion","1.iab units (desktop + mobile) + non-expanding adhesion","1.iab units (desktop + mobile) + non-expanding adhesion",
"2.iab units (desktop + mobile) + non-expanding adhesion","2.iab units (desktop + mobile) + non-expanding adhesion","2.iab units (desktop + mobile) + non-expanding adhesion","10.iab units (desktop + mobile) + non-expanding adhesion","10.iab units (desktop + mobile) + non-expanding adhesion","10.iab units (desktop + mobile) + non-expanding adhesion"],"Number":["361766","4280","275853","275853","73582","4024","8910","568861","85658"]}

data = PD.DataFrame(d)
print data



                                               ABC  Number
0  1.iab units (desktop + mobile) + non-expanding...  361766
1  1.iab units (desktop + mobile) + non-expanding...    4280
2  1.iab units (desktop + mobile) + non-expanding...  275853
3  2.iab units (desktop + mobile) + non-expanding...  275853
4  2.iab units (desktop + mobile) + non-expanding...   73582
5  2.iab units (desktop + mobile) + non-expanding...    4024
6  10.iab units (desktop + mobile) + non-expandin...    8910
7  10.iab units (desktop + mobile) + non-expandin...  568861
8  10.iab units (desktop + mobile) + non-expandin...   85658

在应用组时,它将以以下形式排序:

for plc, plc_name in data.groupby('ABC'):
  print plc_name

                                   ABC  Number
0  1.iab units (desktop + mobile) + non-expanding...  361766
1  1.iab units (desktop + mobile) + non-expanding...    4280
2  1.iab units (desktop + mobile) + non-expanding...  275853
                                                 ABC  Number
6  10.iab units (desktop + mobile) + non-expandin...    8910
7  10.iab units (desktop + mobile) + non-expandin...  568861
8  10.iab units (desktop + mobile) + non-expandin...   85658
                                                 ABC  Number
3  2.iab units (desktop + mobile) + non-expanding...  275853
4  2.iab units (desktop + mobile) + non-expanding...   73582
5  2.iab units (desktop + mobile) + non-expanding...    4024

有什么办法不让我喜欢吗?

ABC Number
1.iab units (desktop + mobile) + non-expanding adhesion 361766
1.iab units (desktop + mobile) + non-expanding adhesion 4280
1.iab units (desktop + mobile) + non-expanding adhesion 275853
ABC Number
2.iab units (desktop + mobile) + non-expanding adhesion 8910
2.iab units (desktop + mobile) + non-expanding adhesion 568861
2.iab units (desktop + mobile) + non-expanding adhesion 85658
ABC Number
10.iab units (desktop + mobile) + non-expanding adhesion    346177
10.iab units (desktop + mobile) + non-expanding adhesion    73582
10.iab units (desktop + mobile) + non-expanding adhesion    4024

1 个答案:

答案 0 :(得分:2)

groupby sort = False

for plc, plc_name in data.groupby('ABC',sort=False):
  print(plc_name)

                                                 ABC  Number
0  1.iab units (desktop + mobile) + non-expanding...  361766
1  1.iab units (desktop + mobile) + non-expanding...    4280
2  1.iab units (desktop + mobile) + non-expanding...  275853
                                                 ABC  Number
3  2.iab units (desktop + mobile) + non-expanding...  275853
4  2.iab units (desktop + mobile) + non-expanding...   73582
5  2.iab units (desktop + mobile) + non-expanding...    4024
                                                 ABC  Number
6  10.iab units (desktop + mobile) + non-expandin...    8910
7  10.iab units (desktop + mobile) + non-expandin...  568861
8  10.iab units (desktop + mobile) + non-expandin...   85658