{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pandas Review" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "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", " \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", "
countrybeer_servingsspirit_servingswine_servingstotal_litres_of_pure_alcoholcontinent
0Afghanistan0000.0AS
1Albania89132544.9EU
2Algeria250140.7AF
3Andorra24513831212.4EU
4Angola21757455.9AF
\n", "
" ], "text/plain": [ " country beer_servings spirit_servings wine_servings \\\n", "0 Afghanistan 0 0 0 \n", "1 Albania 89 132 54 \n", "2 Algeria 25 0 14 \n", "3 Andorra 245 138 312 \n", "4 Angola 217 57 45 \n", "\n", " total_litres_of_pure_alcohol continent \n", "0 0.0 AS \n", "1 4.9 EU \n", "2 0.7 AF \n", "3 12.4 EU \n", "4 5.9 AF " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/drinks.csv'\n", "df = pd.read_csv(url).head(5).copy()\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each of the following lines of code:\n", "\n", "- What the **data type** of the object that is returned?\n", "- What is the **shape** of the object that is returned?\n", "\n", "\n", "1. `df`\n", "2. `df.continent`\n", "3. `df['continent']`\n", "4. `df[['country', 'continent']]`\n", "5. `df[[False, True, False, True, False]]`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question 1" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "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", " \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", "
countrybeer_servingsspirit_servingswine_servingstotal_litres_of_pure_alcoholcontinent
0Afghanistan0000.0AS
1Albania89132544.9EU
2Algeria250140.7AF
3Andorra24513831212.4EU
4Angola21757455.9AF
\n", "
" ], "text/plain": [ " country beer_servings spirit_servings wine_servings \\\n", "0 Afghanistan 0 0 0 \n", "1 Albania 89 132 54 \n", "2 Algeria 25 0 14 \n", "3 Andorra 245 138 312 \n", "4 Angola 217 57 45 \n", "\n", " total_litres_of_pure_alcohol continent \n", "0 0.0 AS \n", "1 4.9 EU \n", "2 0.7 AF \n", "3 12.4 EU \n", "4 5.9 AF " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(5, 6)\n" ] } ], "source": [ "print type(df)\n", "print df.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question 2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 AS\n", "1 EU\n", "2 AF\n", "3 EU\n", "4 AF\n", "Name: continent, dtype: object" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.continent" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(5L,)\n" ] } ], "source": [ "print type(df.continent)\n", "print df.continent.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question 3" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0 AS\n", "1 EU\n", "2 AF\n", "3 EU\n", "4 AF\n", "Name: continent, dtype: object" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['continent']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(5L,)\n" ] } ], "source": [ "print type(df['continent'])\n", "print df['continent'].shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question 4" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countrycontinent
0AfghanistanAS
1AlbaniaEU
2AlgeriaAF
3AndorraEU
4AngolaAF
\n", "
" ], "text/plain": [ " country continent\n", "0 Afghanistan AS\n", "1 Albania EU\n", "2 Algeria AF\n", "3 Andorra EU\n", "4 Angola AF" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[['country', 'continent']]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(5, 2)\n" ] } ], "source": [ "print type(df[['country', 'continent']])\n", "print df[['country', 'continent']].shape" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countrycontinent
0AfghanistanAS
1AlbaniaEU
2AlgeriaAF
3AndorraEU
4AngolaAF
\n", "
" ], "text/plain": [ " country continent\n", "0 Afghanistan AS\n", "1 Albania EU\n", "2 Algeria AF\n", "3 Andorra EU\n", "4 Angola AF" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# equivalent\n", "cols = ['country', 'continent']\n", "df[cols]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Question 5" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countrybeer_servingsspirit_servingswine_servingstotal_litres_of_pure_alcoholcontinent
1Albania89132544.9EU
3Andorra24513831212.4EU
\n", "
" ], "text/plain": [ " country beer_servings spirit_servings wine_servings \\\n", "1 Albania 89 132 54 \n", "3 Andorra 245 138 312 \n", "\n", " total_litres_of_pure_alcohol continent \n", "1 4.9 EU \n", "3 12.4 EU " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[[False, True, False, True, False]]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "(2, 6)\n" ] } ], "source": [ "print type(df[[False, True, False, True, False]])\n", "print df[[False, True, False, True, False]].shape" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countrybeer_servingsspirit_servingswine_servingstotal_litres_of_pure_alcoholcontinent
1Albania89132544.9EU
3Andorra24513831212.4EU
\n", "
" ], "text/plain": [ " country beer_servings spirit_servings wine_servings \\\n", "1 Albania 89 132 54 \n", "3 Andorra 245 138 312 \n", "\n", " total_litres_of_pure_alcohol continent \n", "1 4.9 EU \n", "3 12.4 EU " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# equivalent\n", "df[df.continent=='EU']" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }