我有一个简单的 Pandas Dataframe,名为purchase_cat_df:
email cat
0 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="157078747c7924557278747c793b767a78" rel="noreferrer noopener nofollow">[email protected]</a> Mobiles & Tablets
1 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6b636f67623c4e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a> Mobiles & Tablets
2 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a2aaa6aeabf687a0aaa6aeabe9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a> Mobiles & Tablets
3 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c3cbc7cfca95e6c1cbc7cfca88c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a> Mobiles & Tablets
4 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a2f272b2326790a2d272b232664292527" rel="noreferrer noopener nofollow">[email protected]</a> Home & Living
5 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a2f272b23267b0a2d272b232664292527" rel="noreferrer noopener nofollow">[email protected]</a> Home & Living
我按“电子邮件”进行分组,并将“猫”放入这样的列表中:
test = purchase_cat_df.groupby('email').apply(lambda x: list(x.cat))
但是我的 DataFrame 测试是:
email
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afcac2cec6c39eefc8c2cec6c381ccc0c2" rel="noreferrer noopener nofollow">[email protected]</a> [Mobiles & Tablets, Mobiles & Tablets, Home & ...
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8ada5a9a1a4fa88afa5a9a1a4e6aba7a5" rel="noreferrer noopener nofollow">[email protected]</a> [Mobiles & Tablets]
<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7e767a7277285b7c767a727735787476" rel="noreferrer noopener nofollow">[email protected]</a> [Mobiles & Tablets, Home & Living]
我丢失了索引和名称,如何命名列 2?
最佳答案
如果您想保留原始索引,您可能正在寻找如下内容:
purchase_cat_df.groupby('email', as_index=False)
as_index=False 保留原始索引。然后,您可以继续通过名称来寻址该列。
关于python - pandas,按函数分组后的列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26002474/