{ "cells": [ { "cell_type": "markdown", "metadata": {}, "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/creating-reading-and-writing).**\n", "\n", "---\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "The first step in most data analytics projects is reading the data file. In this exercise, you'll create Series and DataFrame objects, both by hand and by reading data files.\n", "\n", "Run the code cell below to load libraries you will need (including code to check your answers)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Setup complete.\n" ] } ], "source": [ "import pandas as pd\n", "pd.set_option('max_rows', 5)\n", "from learntools.core import binder; binder.bind(globals())\n", "from learntools.pandas.creating_reading_and_writing import *\n", "print(\"Setup complete.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.\n", "\n", "In the cell below, create a DataFrame `fruits` that looks like this:\n", "\n", "![](https://i.imgur.com/Ax3pp2A.png)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"1_FruitDfCreation\", \"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", "
ApplesBananas
03021
\n", "
" ], "text/plain": [ " Apples Bananas\n", "0 30 21" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code goes here. Create a dataframe matching the above diagram and assign it to the variable fruits.\n", "fruits = pd.DataFrame({'Apples': 30, 'Bananas': 21}, index=[0])\n", "\n", "# Check your answer\n", "q1.check()\n", "fruits" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "q1.hint()\n", "#q1.solution()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2.\n", "\n", "Create a dataframe `fruit_sales` that matches the diagram below:\n", "\n", "![](https://i.imgur.com/CHPn7ZF.png)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"2_FruitSalesDfCreation\", \"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", "
ApplesBananas
2017 Sales3521
2018 Sales4134
\n", "
" ], "text/plain": [ " Apples Bananas\n", "2017 Sales 35 21\n", "2018 Sales 41 34" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Your code goes here. Create a dataframe matching the above diagram and assign it to the variable fruit_sales.\n", "fruit_sales = pd.DataFrame({'Apples': [35, 41], 'Bananas': [21, 34]}, index=['2017 Sales', '2018 Sales'])\n", "\n", "# Check your answer\n", "q2.check()\n", "fruit_sales" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "#q2.hint()\n", "#q2.solution()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3.\n", "\n", "Create a variable `ingredients` with a Series that looks like:\n", "\n", "```\n", "Flour 4 cups\n", "Milk 1 cup\n", "Eggs 2 large\n", "Spam 1 can\n", "Name: Dinner, dtype: object\n", "```" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 2, \"questionId\": \"3_RecipeSeriesCreation\", \"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": [ "Flour 4 cups\n", "Milk 1 cup\n", "Eggs 2 large\n", "Spam 1 can\n", "Name: Dinner, dtype: object" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ingredients = pd.Series(data={'Flour': '4 cups', 'Milk': '1 cup', 'Eggs': '2 large', 'Spam': '1 can'}, name='Dinner')\n", "\n", "# Check your answer\n", "q3.check()\n", "ingredients" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"interactionType\": 2, \"questionType\": 2, \"questionId\": \"3_RecipeSeriesCreation\", \"learnToolsVersion\": \"0.3.4\", \"valueTowardsCompletion\": 0.0, \"failureMessage\": \"\", \"exceptionClass\": \"\", \"trace\": \"\", \"outcomeType\": 4}}, \"*\")" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Hint: Note that the Series must be named `\"Dinner\"`. Use the `name` keyword-arg when creating your series." ], "text/plain": [ "Hint: Note that the Series must be named `\"Dinner\"`. Use the `name` keyword-arg when creating your series." ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "q3.hint()\n", "#q3.solution()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4.\n", "\n", "Read the following csv dataset of wine reviews into a DataFrame called `reviews`:\n", "\n", "![](https://i.imgur.com/74RCZtU.png)\n", "\n", "The filepath to the csv file is `../input/wine-reviews/winemag-data_first150k.csv`. The first few lines look like:\n", "\n", "```\n", ",country,description,designation,points,price,province,region_1,region_2,variety,winery\n", "0,US,\"This tremendous 100% varietal wine[...]\",Martha's Vineyard,96,235.0,California,Napa Valley,Napa,Cabernet Sauvignon,Heitz\n", "1,Spain,\"Ripe aromas of fig, blackberry and[...]\",Carodorum Selección Especial Reserva,96,110.0,Northern Spain,Toro,,Tinta de Toro,Bodega Carmen Rodríguez\n", "```" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 1, \"questionId\": \"4_ReadWineCsv\", \"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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countrydescriptiondesignationpointspriceprovinceregion_1region_2varietywinery
0USThis tremendous 100% varietal wine hails from ...Martha's Vineyard96235.0CaliforniaNapa ValleyNapaCabernet SauvignonHeitz
1SpainRipe aromas of fig, blackberry and cassis are ...Carodorum Selección Especial Reserva96110.0Northern SpainToroNaNTinta de ToroBodega Carmen Rodríguez
.................................
150928FranceA perfect salmon shade, with scents of peaches...Grand Brut Rosé9052.0ChampagneChampagneNaNChampagne BlendGosset
150929ItalyMore Pinot Grigios should taste like this. A r...NaN9015.0Northeastern ItalyAlto AdigeNaNPinot GrigioAlois Lageder
\n", "

150930 rows × 10 columns

\n", "
" ], "text/plain": [ " country description \\\n", "0 US This tremendous 100% varietal wine hails from ... \n", "1 Spain Ripe aromas of fig, blackberry and cassis are ... \n", "... ... ... \n", "150928 France A perfect salmon shade, with scents of peaches... \n", "150929 Italy More Pinot Grigios should taste like this. A r... \n", "\n", " designation points price \\\n", "0 Martha's Vineyard 96 235.0 \n", "1 Carodorum Selección Especial Reserva 96 110.0 \n", "... ... ... ... \n", "150928 Grand Brut Rosé 90 52.0 \n", "150929 NaN 90 15.0 \n", "\n", " province region_1 region_2 variety \\\n", "0 California Napa Valley Napa Cabernet Sauvignon \n", "1 Northern Spain Toro NaN Tinta de Toro \n", "... ... ... ... ... \n", "150928 Champagne Champagne NaN Champagne Blend \n", "150929 Northeastern Italy Alto Adige NaN Pinot Grigio \n", "\n", " winery \n", "0 Heitz \n", "1 Bodega Carmen Rodríguez \n", "... ... \n", "150928 Gosset \n", "150929 Alois Lageder \n", "\n", "[150930 rows x 10 columns]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reviews = pd.read_csv('../input/wine-reviews/winemag-data_first150k.csv', index_col=0)\n", "\n", "# Check your answer\n", "q4.check()\n", "reviews" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "#q4.hint()\n", "#q4.solution()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.\n", "\n", "Run the cell below to create and display a DataFrame called `animals`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "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", "
CowsGoats
Year 11222
Year 22019
\n", "
" ], "text/plain": [ " Cows Goats\n", "Year 1 12 22\n", "Year 2 20 19" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "animals = pd.DataFrame({'Cows': [12, 20], 'Goats': [22, 19]}, index=['Year 1', 'Year 2'])\n", "animals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the cell below, write code to save this DataFrame to disk as a csv file with the name `cows_and_goats.csv`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "parent.postMessage({\"jupyterEvent\": \"custom.exercise_interaction\", \"data\": {\"outcomeType\": 1, \"valueTowardsCompletion\": 0.16666666666666666, \"interactionType\": 1, \"questionType\": 2, \"questionId\": \"5_SaveAnimalsCsv\", \"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" } ], "source": [ "# Your code goes here\n", "animals.to_csv('cows_and_goats.csv')\n", "# Check your answer\n", "q5.check()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "#q5.hint()\n", "#q5.solution()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Keep going\n", "\n", "Move on to learn about **[indexing, selecting and assigning](https://www.kaggle.com/residentmario/indexing-selecting-assigning)**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "\n", "\n", "\n", "*Have questions or comments? Visit the [Learn Discussion forum](https://www.kaggle.com/learn-forum/161299) to chat with other Learners.*" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.6" } }, "nbformat": 4, "nbformat_minor": 4 }