<jupyter_start><jupyter_text><jupyter_code>import os

import numpy as np
import pandas as pd

from getpass import getpass 
def access_kaggle():
    """
    Access Kaggle from Google Colab.
    If the /root/.kaggle does not exist then prompt for
    the username and for the Kaggle API key.
    Creates the kaggle.json access file in the /root/.kaggle/ folder. 
    """
    KAGGLE_ROOT = os.path.join('/root', '.kaggle')
    KAGGLE_PATH = os.path.join(KAGGLE_ROOT, 'kaggle.json')

    if '.kaggle' not in os.listdir(path='/root'):
        user = getpass(prompt='Kaggle username: ')
        key  = getpass(prompt='Kaggle API key: ')
        
        !mkdir $KAGGLE_ROOT
        !touch $KAGGLE_PATH
        !chmod 666 $KAGGLE_PATH
        with open(KAGGLE_PATH, mode='w') as f:
            f.write('{"username":"%s", "key":"%s"}' %(user, key))
            f.close()
        !chmod 600 $KAGGLE_PATH
        del user
        del key
        success_msg = "Kaggle is successfully set up. Good to go."
        print(f'{success_msg}')

access_kaggle()

!kaggle datasets download zynicide/wine-reviews --unzip -p datasets
!ls -lh datasets

df = pd.read_csv('datasets/winemag-data-130k-v2.csv', sep=",",
                 index_col=0)
df.shape
df.head(3)<jupyter_output><empty_output><jupyter_text>IntroductionIn these exercises we'll apply groupwise analysis to our dataset.Run the code cell below to load the data before running the exercises.  Exercises  1.Who are the most common wine reviewers in the dataset? Create a `Series` whose index is the `taster_twitter_handle` category from the dataset, and whose values count how many reviews each person wrote.<jupyter_code>df.columns
df.groupby(by=['taster_twitter_handle']).size()<jupyter_output><empty_output><jupyter_text>2.What is the best wine I can buy for a given amount of money? Create a `Series` whose index is wine prices and whose values is the maximum number of points a wine costing that much was given in a review. Sort the values by price, ascending (so that `4.0` dollars is at the top and `3300.0` dollars is at the bottom).<jupyter_code>df.groupby(by=['price']).points.max().sort_index(ascending=True)<jupyter_output><empty_output><jupyter_text>3.What are the minimum and maximum prices for each `variety` of wine? Create a `DataFrame` whose index is the `variety` category from the dataset and whose values are the `min` and `max` values thereof.<jupyter_code>min_max_variety_df = df.groupby(by=['variety']).price.agg(['min', 'max'])
min_max_variety_df<jupyter_output><empty_output><jupyter_text>4.What are the most expensive wine varieties? Create a variable `sorted_varieties` containing a copy of the dataframe from the previous question where varieties are sorted in descending order based on minimum price, then on maximum price (to break ties).<jupyter_code>sorted_varieties = min_max_variety_df.sort_values(by=['min', 'max'],
                                                  ascending=[False, False])
sorted_varieties<jupyter_output><empty_output><jupyter_text>5.Create a `Series` whose index is reviewers and whose values is the average review score given out by that reviewer. Hint: you will need the `taster_name` and `points` columns.<jupyter_code>reviewer_mean_ratings = df.groupby(by=['taster_name']).points.mean()
reviewer_mean_ratings<jupyter_output><empty_output><jupyter_text>Are there significant differences in the average scores assigned by the various reviewers? Run the cell below to use the `describe()` method to see a summary of the range of values.<jupyter_code>reviewer_mean_ratings.describe()<jupyter_output><empty_output><jupyter_text>6.What combination of countries and varieties are most common? Create a `Series` whose index is a `MultiIndex`of `{country, variety}` pairs. For example, a pinot noir produced in the US should map to `{"US", "Pinot Noir"}`. Sort the values in the `Series` in descending order based on wine count.<jupyter_code>df.groupby(by=['country', 'variety']).size()
# country_variety_counts = ____<jupyter_output><empty_output>