gpt4 book ai didi

python - 在 Pandas 中分组,同时保留元组

转载 作者:太空宇宙 更新时间:2023-11-03 17:02:35 25 4
gpt4 key购买 nike

我有一个看起来像这样的数据框(实际上有 35 列和更多元组,但下面是相关列:

     leg_side  leg_quantity expiration product  change_type  
0 None None None ZQ inserted
1 None None None HG inserted
2 None None None PL inserted
3 None None None SI inserted
4 None None None ZQ inserted
5 None None None PL inserted
6 None None None ZW inserted
7 None None None SI inserted
8 None None None ZQ updated
9 None None None SI inserted
10 None None None ZC updated
.. ... ... ... ... ...
970 None None None OZ inserted
971 None None None OZ deleted
972 None None None OZ updated
973 None None None ZC inserted
974 None None None OZ inserted
975 None None None ZC inserted
976 None None None OZ inserted

现在我想做的是按产品分组,但不一定是在 SQL 意义上。我想做的只是将具有相似产品的所有元组聚合在一起,并通过change_type进行子聚合,以获得如下所示的df:

     leg_side  leg_quantity expiration product  change_type  
0 None None None ZQ inserted
4 None None None ZQ inserted
8 None None None ZQ updated
1 None None None HG inserted
2 None None None PL inserted
5 None None None PL inserted
3 None None None SI inserted
7 None None None SI inserted
9 None None None SI inserted
6 None None None ZW inserted
...
973 None None None ZC inserted
975 None None None ZC inserted
10 None None None ZC updated
970 None None None OZ inserted
974 None None None OZ inserted
976 None None None OZ inserted
972 None None None OZ updated
971 None None None OZ deleted

上面的数据框的组织方式是,具有相同产品名称的所有元组都放在一起,然后这些组中具有相同更改类型的所有元组都分组在一起(最好按照插入、更新、删除的顺序)。如果我执行 pandas groupby() 那么元组将被消除。我只是想要一种分组排序的感觉。我怎样才能做到这一点?

最佳答案

您可以使用分类set定制订单。然后groupby数据排序:

df['change_type'] = df['change_type'].astype('category')
.cat
.set_categories(["inserted","updated","deleted"], ordered=True)

df = df.groupby('product').apply(lambda x: x.sort_values('change_type'))
.reset_index(drop=True)
print df

leg_side leg_quantity expiration product change_type
0 None None None HG inserted
1 None None None OZ inserted
2 None None None OZ inserted
3 None None None OZ inserted
4 None None None OZ updated
5 None None None OZ deleted
6 None None None PL inserted
7 None None None PL inserted
8 None None None SI inserted
9 None None None SI inserted
10 None None None SI inserted
11 None None None ZC inserted
12 None None None ZC inserted
13 None None None ZC updated
14 None None None ZQ inserted
15 None None None ZQ inserted
16 None None None ZQ updated
17 None None None ZW inserted

关于python - 在 Pandas 中分组,同时保留元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34906328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com