1. First, create a Pandas data frame looks as following:
Name Product Amount
0 Bob apple 1
1 Bob banana 2
2 jessica orange 3
3 jessica banana 4
4 jessica tomato 3
5 mary banana 2
6 john apple 3
7 john grape 1
Second, find out for each product, how many were sold (for example, 4 units of Apple were sold). Third, which products sales are larger than 3 (for example, apple sold more than 3 units)
PYTHON
STEP - BY - STEP PROCESS
(i) Create a Pandas data frame:
import pandas as pd
Name = pd.Series(['Bob', 'Bob', 'Jessica', 'Jessica', 'Jessica', 'Mary', 'John', 'John'])
Product = pd.Series(['Apple', 'Banana', 'Orange', 'Banana', 'Tomato', 'Banana', 'Apple', 'Grape'])
Amount = pd.Series([1, 2, 3, 4, 3, 2, 3, 1])
df = pd.DataFrame([Name, Product, Amount], index=['Name', 'Product', 'Amount']).T
df
(ii) Find out for each product, how many were sold:
df1 = df.groupby(['Product'])['Amount'].agg('sum')
df1
(iii) Find which products sales are larger than 3:
df1[df1 > 3]
Thumbs Up Please !!!
Get Answers For Free
Most questions answered within 1 hours.