本文介绍了 pandas |分组依据,该组的所有值以逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据应用程序的要求,我需要以逗号分隔的格式显示分组依据的所有数据,以便管理员可以做出决定.我是Python的新手,不确定如何做.

As per application requirement, I need to show all the data which is part of group by in comma separated format so the admin can take decision, I am new to Python and not sure how to do it.

可复制的数据样本

import pandas as pd

compnaies = ['Microsoft', 'Google', 'Amazon', 'Microsoft', 'Facebook', 'Google']
products = ['OS', 'Search', 'E-comm', 'X-box', 'Social Media', 'Android']

df = pd.DataFrame({'company' : compnaies, 'product':products })
-----------------------------------------------------------------   
    company     product
0   Microsoft   OS
1   Google      Search
2   Amazon      E-comm
3   Microsoft   X-box
4   Facebook    Social Media
5   Google      Android

现在我通过此代码获得了公司集团的计数

Now I getting count of company group by this code

df.groupby(['company']).count()

我需要以下提到的格式的数据,但不确定如何获取数据

I need data in below mentioned format but not sure how to get it

所需的输出

company    count product
Amazon      1    E-comm
Facebook    1    Social Media
Google      2    Search, Android
Microsoft   2    OS, X-box

推荐答案

您可以使用:

In [35]: df.groupby('company').product.agg([('count', 'count'), ('product', ', '.join)])
Out[35]: 
           count          product
company                          
Amazon         1           E-comm
Facebook       1     Social Media
Google         2  Search, Android
Microsoft      2        OS, X-box

这篇关于 pandas |分组依据,该组的所有值以逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 07:11