Week 5 (2/28-3/6)¶
Notebook¶
Download the notebook file: week_5_class.ipynb.
Weekly digest¶
Pandas¶
GroupBy
Index operations
Resorces¶
1. Seaborn sample data¶
Exercises¶
All exercises below use data on restaurant visits. A DataFrame with this data can be created as follows:
[23]:
import seaborn as sns
tips = sns.load_dataset("tips")
tips.head(5)
[23]:
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Exercise 1¶
How many people visited the restaurant each day of the week?
Check: There were 152 visitors on Thursday.
Exercise 2¶
What was the average bill paid each day of the week?
Check: The average bill on Thursday was $17.48.
Exercise 3¶
For each restaurant visit, compute what percentage of the total bill was the tip, and then compute the average value of this percentage for male and for female customers.
Check: The average tip for males was 15.76%.
Exercise 4¶
Compute a DateFrame with 10 rows selected from the tips data: 5 rows with the the 5 highest total bills for smokers and 5 rows with the 5 highest total bills for non-smokers
Check: The 5 highest bills for smokers were $50.81, $43.35, $44.30, $43.11 and $40.55.
Exercise 5¶
Create a DataFrame with 4 rows selected from the tips data: a row with the highest total bill for a male smoker, a row with the highest total bill for a male non-smoker, and the same for a female smokers and a female non-smokers.
Check: The highest bills were $50.81 for male smoker, $48.33 for a male non-smoker, $44.30 for a female smoker and $35.83 for a female non-smoker.
Exercise 6¶
Create a DataFrame with two rows indicating if a customer was a male or a female, and columns indicating which day of the week the customer visited the restaurant. The value in a given row and column should be the amount of the largest tip that a customer of a given sex and visiting on a given day paid.
Check: The highest tip paid on Thursday was $6.70 for males and $5.17 for females.