Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas | Group by with all the values of the group as comma separated

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.

Sample reproducible data

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

Desired output

company    count product
Amazon      1    E-comm
Facebook    1    Social Media
Google      2    Search, Android
Microsoft   2    OS, X-box
like image 559
Vineet Avatar asked May 19 '18 07:05

Vineet


People also ask

How Split Comma Separated Values in pandas?

First, we'll split or “explode” our string of comma separated values into a Python list using the str. split() function. We'll append the str. split() function to the name of the original column and define the separator as a comma, then we'll assign the output of the result to a new column called models_list .

Can I use group by without aggregate function pandas?

Instead of using groupby aggregation together, we can perform groupby without aggregation which is applicable to aggregate data separately.

What is group by () in pandas library?

Pandas groupby is used for grouping the data according to the categories and apply a function to the categories. It also helps to aggregate data efficiently. Pandas dataframe. groupby() function is used to split the data into groups based on some criteria. pandas objects can be split on any of their axes.

Can you use Groupby with multiple columns in pandas?

groupby() can take the list of columns to group by multiple columns and use the aggregate functions to apply single or multiple aggregations at the same time.


1 Answers

You can use:

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
like image 197
llllllllll Avatar answered Oct 13 '22 01:10

llllllllll