pandas ,按功能分组后的列名称 [英] pandas, name of the column after a group by function

查看:106
本文介绍了 pandas ,按功能分组后的列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的名为pu​​rchase_cat_df的Pandas Dataframe:

I have a simple Pandas Dataframe named purchase_cat_df:

              email                cat
0  email1@gmail.com  Mobiles & Tablets
1  email2@gmail.com  Mobiles & Tablets
2  email1@gmail.com  Mobiles & Tablets
3  email3@gmail.com  Mobiles & Tablets
4  email3@gmail.com      Home & Living
5  email1@gmail.com      Home & Living

我按照'email'分组,并将'cat'放入这样的列表中:

I'm grouping by the 'email' and and putting 'cat' in a list like this:

test = purchase_cat_df.groupby('email').apply(lambda x: list(x.cat))

但是我的DataFrame测试是:

but then my DataFrame test is:

email
email1@gmail.com    [Mobiles & Tablets, Mobiles & Tablets, Home & ...
email2@gmail.com                                  [Mobiles & Tablets]
email3@gmail.com                   [Mobiles & Tablets, Home & Living]

我失去了索引和名字,我怎么能够命名第2列?

I lost the indexs and the name, how can I named the column 2?

推荐答案

如果你想保留你的原始索引,你可能正在寻找这样的东西:

If you want to keep your original index, you were probably looking for something like this:

purchase_cat_df.groupby('email', as_index=False)

as_index = False保留原始索引。然后,您可以继续按名称寻址该列。

as_index=False keeps the original index. You can then continue to address the column by its name.

这篇关于 pandas ,按功能分组后的列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆