From 719471466226af9d38c17ccec9fea2cfa0419dae Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Tue, 5 Apr 2022 19:52:31 +0530 Subject: [PATCH] Kaggle Notebook | Pandas - 4 Exercise: Grouping and Sorting | Version 1 --- pandas-4-exercise-grouping-and-sorting.ipynb | 1 + 1 file changed, 1 insertion(+) create mode 100644 pandas-4-exercise-grouping-and-sorting.ipynb diff --git a/pandas-4-exercise-grouping-and-sorting.ipynb b/pandas-4-exercise-grouping-and-sorting.ipynb new file mode 100644 index 0000000..1178446 --- /dev/null +++ b/pandas-4-exercise-grouping-and-sorting.ipynb @@ -0,0 +1 @@ +{"cells":[{"source":"\"Kaggle\"","metadata":{},"cell_type":"markdown","outputs":[],"execution_count":0},{"cell_type":"markdown","id":"feb287d5","metadata":{"papermill":{"duration":0.020164,"end_time":"2022-04-05T14:22:12.350312","exception":false,"start_time":"2022-04-05T14:22:12.330148","status":"completed"},"tags":[]},"source":["**This notebook is an exercise in the [Pandas](https://www.kaggle.com/learn/pandas) course. You can reference the tutorial at [this link](https://www.kaggle.com/residentmario/grouping-and-sorting).**\n","\n","---\n"]},{"cell_type":"markdown","id":"2ff43159","metadata":{"papermill":{"duration":0.018286,"end_time":"2022-04-05T14:22:12.387312","exception":false,"start_time":"2022-04-05T14:22:12.369026","status":"completed"},"tags":[]},"source":["# Introduction\n","\n","In these exercises we'll apply groupwise analysis to our dataset.\n","\n","Run the code cell below to load the data before running the exercises."]},{"cell_type":"code","execution_count":1,"id":"5307e45c","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:12.431984Z","iopub.status.busy":"2022-04-05T14:22:12.427001Z","iopub.status.idle":"2022-04-05T14:22:15.030907Z","shell.execute_reply":"2022-04-05T14:22:15.030205Z","shell.execute_reply.started":"2022-04-05T14:19:48.505891Z"},"papermill":{"duration":2.625584,"end_time":"2022-04-05T14:22:15.031047","exception":false,"start_time":"2022-04-05T14:22:12.405463","status":"completed"},"tags":[]},"outputs":[{"name":"stdout","output_type":"stream","text":["Setup complete.\n"]}],"source":["import pandas as pd\n","\n","reviews = pd.read_csv(\"../input/wine-reviews/winemag-data-130k-v2.csv\", index_col=0)\n","#pd.set_option(\"display.max_rows\", 5)\n","\n","from learntools.core import binder; binder.bind(globals())\n","from learntools.pandas.grouping_and_sorting import *\n","print(\"Setup complete.\")"]},{"cell_type":"markdown","id":"f819f8b8","metadata":{"papermill":{"duration":0.018254,"end_time":"2022-04-05T14:22:15.068554","exception":false,"start_time":"2022-04-05T14:22:15.0503","status":"completed"},"tags":[]},"source":["# Exercises"]},{"cell_type":"markdown","id":"e97d0d97","metadata":{"papermill":{"duration":0.018431,"end_time":"2022-04-05T14:22:15.106275","exception":false,"start_time":"2022-04-05T14:22:15.087844","status":"completed"},"tags":[]},"source":["## 1.\n","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."]},{"cell_type":"code","execution_count":2,"id":"fbcb43b3","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.16245Z","iopub.status.busy":"2022-04-05T14:22:15.161591Z","iopub.status.idle":"2022-04-05T14:22:15.184154Z","shell.execute_reply":"2022-04-05T14:22:15.183741Z","shell.execute_reply.started":"2022-04-05T14:19:49.586668Z"},"papermill":{"duration":0.05908,"end_time":"2022-04-05T14:22:15.184269","exception":false,"start_time":"2022-04-05T14:22:15.125189","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"1_ReviewsWritten\", \"learnToolsVersion\": \"0.3.4\", \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\"}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Correct: \n","\n","\n","```python\n","reviews_written = reviews.groupby('taster_twitter_handle').size()\n","```\n","or\n","```python\n","reviews_written = reviews.groupby('taster_twitter_handle').taster_twitter_handle.count()\n","```\n"],"text/plain":["Correct: \n","\n","\n","```python\n","reviews_written = reviews.groupby('taster_twitter_handle').size()\n","```\n","or\n","```python\n","reviews_written = reviews.groupby('taster_twitter_handle').taster_twitter_handle.count()\n","```"]},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":["taster_twitter_handle\n","@AnneInVino 3685\n","@JoeCz 5147\n","@bkfiona 27\n","@gordone_cellars 4177\n","@kerinokeefe 10776\n","@laurbuzz 1835\n","@mattkettmann 6332\n","@paulgwine  9532\n","@suskostrzewa 1085\n","@vboone 9537\n","@vossroger 25514\n","@wawinereport 4966\n","@wineschach 15134\n","@winewchristina 6\n","@worldwineguys 1005\n","Name: taster_twitter_handle, dtype: int64"]},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":["# Your code here\n","reviews_written = reviews.groupby('taster_twitter_handle').taster_twitter_handle.count()\n","\n","# Check your answer\n","q1.check()\n","reviews_written"]},{"cell_type":"code","execution_count":3,"id":"f838fdcd","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.227775Z","iopub.status.busy":"2022-04-05T14:22:15.227267Z","iopub.status.idle":"2022-04-05T14:22:15.229789Z","shell.execute_reply":"2022-04-05T14:22:15.230237Z","shell.execute_reply.started":"2022-04-05T14:19:49.632559Z"},"papermill":{"duration":0.025553,"end_time":"2022-04-05T14:22:15.23038","exception":false,"start_time":"2022-04-05T14:22:15.204827","status":"completed"},"tags":[]},"outputs":[],"source":["#q1.hint()\n","#q1.solution()"]},{"cell_type":"markdown","id":"1ff2e4ef","metadata":{"papermill":{"duration":0.020227,"end_time":"2022-04-05T14:22:15.270436","exception":false,"start_time":"2022-04-05T14:22:15.250209","status":"completed"},"tags":[]},"source":["## 2.\n","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)."]},{"cell_type":"code","execution_count":4,"id":"239ed67c","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.315296Z","iopub.status.busy":"2022-04-05T14:22:15.314698Z","iopub.status.idle":"2022-04-05T14:22:15.327908Z","shell.execute_reply":"2022-04-05T14:22:15.328307Z","shell.execute_reply.started":"2022-04-05T14:19:49.638638Z"},"papermill":{"duration":0.037927,"end_time":"2022-04-05T14:22:15.328454","exception":false,"start_time":"2022-04-05T14:22:15.290527","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"2_BestRatingPerPrice\", \"learnToolsVersion\": \"0.3.4\", \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\"}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Correct"],"text/plain":["Correct"]},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":["price\n","4.0 86\n","5.0 87\n","6.0 88\n","7.0 91\n","8.0 91\n"," ..\n","1900.0 98\n","2000.0 97\n","2013.0 91\n","2500.0 96\n","3300.0 88\n","Name: points, Length: 390, dtype: int64"]},"execution_count":4,"metadata":{},"output_type":"execute_result"}],"source":["best_rating_per_price = reviews.groupby('price')['points'].max().sort_index()\n","\n","# Check your answer\n","q2.check()\n","best_rating_per_price"]},{"cell_type":"code","execution_count":5,"id":"63557353","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.376522Z","iopub.status.busy":"2022-04-05T14:22:15.375952Z","iopub.status.idle":"2022-04-05T14:22:15.380679Z","shell.execute_reply":"2022-04-05T14:22:15.381101Z","shell.execute_reply.started":"2022-04-05T14:19:49.664815Z"},"papermill":{"duration":0.030421,"end_time":"2022-04-05T14:22:15.381233","exception":false,"start_time":"2022-04-05T14:22:15.350812","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"interactionType\": 3, \"questionType\": 1, \"questionId\": \"2_BestRatingPerPrice\", \"learnToolsVersion\": \"0.3.4\", \"valueTowardsCompletion\": 0.0, \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\", \"outcomeType\": 4}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Solution: \n","```python\n","best_rating_per_price = reviews.groupby('price')['points'].max().sort_index()\n","```"],"text/plain":["Solution: \n","```python\n","best_rating_per_price = reviews.groupby('price')['points'].max().sort_index()\n","```"]},"metadata":{},"output_type":"display_data"}],"source":["#q2.hint()\n","q2.solution()"]},{"cell_type":"markdown","id":"0ab138ce","metadata":{"papermill":{"duration":0.023455,"end_time":"2022-04-05T14:22:15.427447","exception":false,"start_time":"2022-04-05T14:22:15.403992","status":"completed"},"tags":[]},"source":["## 3.\n","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."]},{"cell_type":"code","execution_count":6,"id":"c7345955","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.482861Z","iopub.status.busy":"2022-04-05T14:22:15.482085Z","iopub.status.idle":"2022-04-05T14:22:15.513077Z","shell.execute_reply":"2022-04-05T14:22:15.51258Z","shell.execute_reply.started":"2022-04-05T14:19:49.673551Z"},"papermill":{"duration":0.062418,"end_time":"2022-04-05T14:22:15.513187","exception":false,"start_time":"2022-04-05T14:22:15.450769","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"3_PriceExtremes\", \"learnToolsVersion\": \"0.3.4\", \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\"}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Correct"],"text/plain":["Correct"]},"metadata":{},"output_type":"display_data"},{"data":{"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
minmax
variety
Abouriou15.075.0
Agiorgitiko10.066.0
Aglianico6.0180.0
Aidani27.027.0
Airen8.010.0
.........
Zinfandel5.0100.0
Zlahtina13.016.0
Zweigelt9.070.0
Çalkarası19.019.0
Žilavka15.015.0
\n","

707 rows × 2 columns

\n","
"],"text/plain":[" min max\n","variety \n","Abouriou 15.0 75.0\n","Agiorgitiko 10.0 66.0\n","Aglianico 6.0 180.0\n","Aidani 27.0 27.0\n","Airen 8.0 10.0\n","... ... ...\n","Zinfandel 5.0 100.0\n","Zlahtina 13.0 16.0\n","Zweigelt 9.0 70.0\n","Çalkarası 19.0 19.0\n","Žilavka 15.0 15.0\n","\n","[707 rows x 2 columns]"]},"execution_count":6,"metadata":{},"output_type":"execute_result"}],"source":["price_extremes = reviews.groupby('variety').price.agg([min,max])\n","\n","# Check your answer\n","q3.check()\n","price_extremes"]},{"cell_type":"code","execution_count":7,"id":"ae871bc3","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.566437Z","iopub.status.busy":"2022-04-05T14:22:15.565893Z","iopub.status.idle":"2022-04-05T14:22:15.56758Z","shell.execute_reply":"2022-04-05T14:22:15.568139Z","shell.execute_reply.started":"2022-04-05T14:19:49.717934Z"},"papermill":{"duration":0.030368,"end_time":"2022-04-05T14:22:15.568283","exception":false,"start_time":"2022-04-05T14:22:15.537915","status":"completed"},"tags":[]},"outputs":[],"source":["#q3.hint()\n","#q3.solution()"]},{"cell_type":"markdown","id":"49861d53","metadata":{"papermill":{"duration":0.024056,"end_time":"2022-04-05T14:22:15.616869","exception":false,"start_time":"2022-04-05T14:22:15.592813","status":"completed"},"tags":[]},"source":["## 4.\n","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)."]},{"cell_type":"code","execution_count":8,"id":"7d4d2763","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.686777Z","iopub.status.busy":"2022-04-05T14:22:15.686173Z","iopub.status.idle":"2022-04-05T14:22:15.703776Z","shell.execute_reply":"2022-04-05T14:22:15.703226Z","shell.execute_reply.started":"2022-04-05T14:19:49.723794Z"},"papermill":{"duration":0.061255,"end_time":"2022-04-05T14:22:15.703892","exception":false,"start_time":"2022-04-05T14:22:15.642637","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"4_SortedVarieties\", \"learnToolsVersion\": \"0.3.4\", \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\"}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Correct"],"text/plain":["Correct"]},"metadata":{},"output_type":"display_data"},{"data":{"text/html":["
\n","\n","\n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n"," \n","
minmax
variety
Ramisco495.0495.0
Terrantez236.0236.0
Francisa160.0160.0
Rosenmuskateller150.0150.0
Tinta Negra Mole112.0112.0
.........
RoscettoNaNNaN
Sauvignon Blanc-Sauvignon GrisNaNNaN
Tempranillo-MalbecNaNNaN
VitalNaNNaN
ZelenNaNNaN
\n","

707 rows × 2 columns

\n","
"],"text/plain":[" min max\n","variety \n","Ramisco 495.0 495.0\n","Terrantez 236.0 236.0\n","Francisa 160.0 160.0\n","Rosenmuskateller 150.0 150.0\n","Tinta Negra Mole 112.0 112.0\n","... ... ...\n","Roscetto NaN NaN\n","Sauvignon Blanc-Sauvignon Gris NaN NaN\n","Tempranillo-Malbec NaN NaN\n","Vital NaN NaN\n","Zelen NaN NaN\n","\n","[707 rows x 2 columns]"]},"execution_count":8,"metadata":{},"output_type":"execute_result"}],"source":["sorted_varieties = reviews.groupby('variety').price.agg([min,max]).sort_values(by=['min', 'max'], ascending=False)\n","\n","# Check your answer\n","q4.check()\n","sorted_varieties"]},{"cell_type":"code","execution_count":9,"id":"301d5521","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.759347Z","iopub.status.busy":"2022-04-05T14:22:15.758765Z","iopub.status.idle":"2022-04-05T14:22:15.761231Z","shell.execute_reply":"2022-04-05T14:22:15.76075Z","shell.execute_reply.started":"2022-04-05T14:19:49.798701Z"},"papermill":{"duration":0.031267,"end_time":"2022-04-05T14:22:15.761369","exception":false,"start_time":"2022-04-05T14:22:15.730102","status":"completed"},"tags":[]},"outputs":[],"source":["#q4.hint()\n","#q4.solution()"]},{"cell_type":"markdown","id":"aa58ea88","metadata":{"papermill":{"duration":0.026272,"end_time":"2022-04-05T14:22:15.813919","exception":false,"start_time":"2022-04-05T14:22:15.787647","status":"completed"},"tags":[]},"source":["## 5.\n","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."]},{"cell_type":"code","execution_count":10,"id":"580acae9","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.901549Z","iopub.status.busy":"2022-04-05T14:22:15.878891Z","iopub.status.idle":"2022-04-05T14:22:15.912996Z","shell.execute_reply":"2022-04-05T14:22:15.912527Z","shell.execute_reply.started":"2022-04-05T14:19:49.805189Z"},"papermill":{"duration":0.068954,"end_time":"2022-04-05T14:22:15.913117","exception":false,"start_time":"2022-04-05T14:22:15.844163","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"5_ReviewerMeanRatings\", \"learnToolsVersion\": \"0.3.4\", \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\"}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Correct"],"text/plain":["Correct"]},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":["taster_name\n","Alexander Peartree 85.855422\n","Anna Lee C. Iijima 88.415629\n","Anne Krebiehl MW 90.562551\n","Carrie Dykes 86.395683\n","Christina Pickard 87.833333\n","Fiona Adams 86.888889\n","Jeff Jenssen 88.319756\n","Jim Gordon 88.626287\n","Joe Czerwinski 88.536235\n","Kerin O’Keefe 88.867947\n","Lauren Buzzeo 87.739510\n","Matt Kettmann 90.008686\n","Michael Schachner 86.907493\n","Mike DeSimone 89.101167\n","Paul Gregutt 89.082564\n","Roger Voss 88.708003\n","Sean P. Sullivan 88.755739\n","Susan Kostrzewa 86.609217\n","Virginie Boone 89.213379\n","Name: points, dtype: float64"]},"execution_count":10,"metadata":{},"output_type":"execute_result"}],"source":["reviewer_mean_ratings = reviews.groupby('taster_name').points.mean()\n","\n","# Check your answer\n","q5.check()\n","reviewer_mean_ratings"]},{"cell_type":"code","execution_count":11,"id":"e73d5686","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:15.979183Z","iopub.status.busy":"2022-04-05T14:22:15.978247Z","iopub.status.idle":"2022-04-05T14:22:15.980757Z","shell.execute_reply":"2022-04-05T14:22:15.980214Z","shell.execute_reply.started":"2022-04-05T14:19:49.843169Z"},"papermill":{"duration":0.035082,"end_time":"2022-04-05T14:22:15.980866","exception":false,"start_time":"2022-04-05T14:22:15.945784","status":"completed"},"tags":[]},"outputs":[],"source":["#q5.hint()\n","#q5.solution()"]},{"cell_type":"markdown","id":"bd256ac5","metadata":{"papermill":{"duration":0.027418,"end_time":"2022-04-05T14:22:16.037227","exception":false,"start_time":"2022-04-05T14:22:16.009809","status":"completed"},"tags":[]},"source":["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."]},{"cell_type":"code","execution_count":12,"id":"4446bc95","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:16.098908Z","iopub.status.busy":"2022-04-05T14:22:16.098256Z","iopub.status.idle":"2022-04-05T14:22:16.104552Z","shell.execute_reply":"2022-04-05T14:22:16.104918Z","shell.execute_reply.started":"2022-04-05T14:19:49.847865Z"},"papermill":{"duration":0.040036,"end_time":"2022-04-05T14:22:16.105054","exception":false,"start_time":"2022-04-05T14:22:16.065018","status":"completed"},"tags":[]},"outputs":[{"data":{"text/plain":["count 19.000000\n","mean 88.233026\n","std 1.243610\n","min 85.855422\n","25% 87.323501\n","50% 88.536235\n","75% 88.975256\n","max 90.562551\n","Name: points, dtype: float64"]},"execution_count":12,"metadata":{},"output_type":"execute_result"}],"source":["reviewer_mean_ratings.describe()"]},{"cell_type":"markdown","id":"b2b1ebc5","metadata":{"papermill":{"duration":0.028225,"end_time":"2022-04-05T14:22:16.161128","exception":false,"start_time":"2022-04-05T14:22:16.132903","status":"completed"},"tags":[]},"source":["## 6.\n","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."]},{"cell_type":"code","execution_count":13,"id":"3f8860c5","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:16.226774Z","iopub.status.busy":"2022-04-05T14:22:16.226128Z","iopub.status.idle":"2022-04-05T14:22:16.268868Z","shell.execute_reply":"2022-04-05T14:22:16.268367Z","shell.execute_reply.started":"2022-04-05T14:19:49.869512Z"},"papermill":{"duration":0.079457,"end_time":"2022-04-05T14:22:16.268998","exception":false,"start_time":"2022-04-05T14:22:16.189541","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"6_GroupbyCountryVariety\", \"learnToolsVersion\": \"0.3.4\", \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\"}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Correct"],"text/plain":["Correct"]},"metadata":{},"output_type":"display_data"},{"data":{"text/plain":["country variety \n","US Pinot Noir 9885\n"," Cabernet Sauvignon 7315\n"," Chardonnay 6801\n","France Bordeaux-style Red Blend 4725\n","Italy Red Blend 3624\n"," ... \n","Mexico Cinsault 1\n"," Grenache 1\n"," Merlot 1\n"," Rosado 1\n","Uruguay White Blend 1\n","Length: 1612, dtype: int64"]},"execution_count":13,"metadata":{},"output_type":"execute_result"}],"source":["country_variety_counts = reviews.groupby(['country','variety']).size().sort_values(ascending=False)\n","\n","# Check your answer\n","q6.check()\n","country_variety_counts"]},{"cell_type":"code","execution_count":14,"id":"ee4b27b8","metadata":{"execution":{"iopub.execute_input":"2022-04-05T14:22:16.334878Z","iopub.status.busy":"2022-04-05T14:22:16.333978Z","iopub.status.idle":"2022-04-05T14:22:16.338732Z","shell.execute_reply":"2022-04-05T14:22:16.338177Z","shell.execute_reply.started":"2022-04-05T14:19:49.923507Z"},"papermill":{"duration":0.038326,"end_time":"2022-04-05T14:22:16.338877","exception":false,"start_time":"2022-04-05T14:22:16.300551","status":"completed"},"tags":[]},"outputs":[{"data":{"application/javascript":["parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"interactionType\": 3, \"questionType\": 1, \"questionId\": \"6_GroupbyCountryVariety\", \"learnToolsVersion\": \"0.3.4\", \"valueTowardsCompletion\": 0.0, \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\", \"outcomeType\": 4}}, \"*\")"],"text/plain":[""]},"metadata":{},"output_type":"display_data"},{"data":{"text/markdown":["Solution: \n","```python\n","country_variety_counts = reviews.groupby(['country', 'variety']).size().sort_values(ascending=False)\n","```"],"text/plain":["Solution: \n","```python\n","country_variety_counts = reviews.groupby(['country', 'variety']).size().sort_values(ascending=False)\n","```"]},"metadata":{},"output_type":"display_data"}],"source":["#q6.hint()\n","q6.solution()"]},{"cell_type":"markdown","id":"d0bfd037","metadata":{"papermill":{"duration":0.03139,"end_time":"2022-04-05T14:22:16.401099","exception":false,"start_time":"2022-04-05T14:22:16.369709","status":"completed"},"tags":[]},"source":["# Keep going\n","\n","Move on to the [**data types and missing data**](https://www.kaggle.com/residentmario/data-types-and-missing-values)."]},{"cell_type":"markdown","id":"28e5558b","metadata":{"papermill":{"duration":0.029836,"end_time":"2022-04-05T14:22:16.460874","exception":false,"start_time":"2022-04-05T14:22:16.431038","status":"completed"},"tags":[]},"source":["---\n","\n","\n","\n","\n","*Have questions or comments? Visit the [course discussion forum](https://www.kaggle.com/learn/pandas/discussion) to chat with other learners.*"]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.12"},"papermill":{"default_parameters":{},"duration":12.557634,"end_time":"2022-04-05T14:22:17.001848","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2022-04-05T14:22:04.444214","version":"2.3.3"}},"nbformat":4,"nbformat_minor":5} \ No newline at end of file