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
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 .
Instead of using groupby aggregation together, we can perform groupby without aggregation which is applicable to aggregate data separately.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With