﻿ Pandas: Relative frequency within each group - w3resource

# Pandas: Relative frequency within each group

## Pandas Grouping and Aggregating: Split-Apply-Combine Exercise-24 with Solution

Write a Pandas program to split the following datasets into groups on customer_id to summarize purch_amt and calculate percentage of purch_amt in each group.

Test Data:

```    ord_no  purch_amt    ord_date  customer_id  salesman_id
0    70001     150.50  05-10-2012         3001         5002
1    70009     270.65  09-10-2012         3001         5005
2    70002      65.26  05-10-2012         3005         5001
3    70004     110.50  08-17-2012         3001         5003
4    70007     948.50  10-09-2012         3005         5002
5    70005    2400.60  07-27-2012         3001         5001
6    70008    5760.00  10-09-2012         3005         5001
7    70010    1983.43  10-10-2012         3001         5006
8    70003    2480.40  10-10-2012         3005         5003
9    70012     250.45  06-17-2012         3001         5002
10   70011      75.29  07-08-2012         3005         5007
11   70013    3045.60  04-25-2012         3005         5001
```

Sample Solution:

Python Code :

``````import pandas as pd
pd.set_option('display.max_rows', None)
#pd.set_option('display.max_columns', None)
df = pd.DataFrame({
'ord_no':[70001,70009,70002,70004,70007,70005,70008,70010,70003,70012,70011,70013],
'purch_amt':[150.5,270.65,65.26,110.5,948.5,2400.6,5760,1983.43,2480.4,250.45, 75.29,3045.6],
'ord_date': ['05-10-2012','09-10-2012','05-10-2012','08-17-2012','10-09-2012','07-27-2012','10-09-2012','10-10-2012','10-10-2012','06-17-2012','07-08-2012','04-25-2012'],
'customer_id':[3001,3001,3005,3001,3005,3001,3005,3001,3005,3001,3005,3005],
'salesman_id': [5002,5005,5001,5003,5002,5001,5001,5006,5003,5002,5007,5001]})
print("Original Orders DataFrame:")
print(df)
gr_data = df.groupby(['customer_id','salesman_id']).agg({'purch_amt': 'sum'})
gr_data["% (Purch Amt.)"] = gr_data.apply(lambda x:  100*x / x.sum())
print("\nPercentage of purch_amt in each group of customer_id:")
print(gr_data)
``````

Sample Output:

```Original Orders DataFrame:
ord_no  purch_amt    ord_date  customer_id  salesman_id
0    70001     150.50  05-10-2012         3001         5002
1    70009     270.65  09-10-2012         3001         5005
2    70002      65.26  05-10-2012         3005         5001
3    70004     110.50  08-17-2012         3001         5003
4    70007     948.50  10-09-2012         3005         5002
5    70005    2400.60  07-27-2012         3001         5001
6    70008    5760.00  10-09-2012         3005         5001
7    70010    1983.43  10-10-2012         3001         5006
8    70003    2480.40  10-10-2012         3005         5003
9    70012     250.45  06-17-2012         3001         5002
10   70011      75.29  07-08-2012         3005         5007
11   70013    3045.60  04-25-2012         3005         5001

Percentage of purch_amt in each group of customer_id:
purch_amt  % (Purch Amt.)
customer_id salesman_id
3001        5001           2400.60       13.685510
5002            400.95        2.285764
5003            110.50        0.629946
5005            270.65        1.542941
5006           1983.43       11.307278
3005        5001           8870.86       50.571626
5002            948.50        5.407276
5003           2480.40       14.140440
5007             75.29        0.429219
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Next: Write a Pandas program to split a dataset, group by one column and get mean, min, and max values by group, also change the column name of the aggregated metric. Using the following dataset find the mean, min, and max values of purchase amount (purch_amt) group by customer id (customer_id).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Python: Tips of the Day

Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value:

Example:

```def tips_map_dictionary(itr, fn):
ret = {}
for a in itr:
ret[a] = fn(a)
return ret
print(tips_map_dictionary([2,4,6], lambda a: a * a))
```

Output:

```{2: 4, 4: 16, 6: 36}
```

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook