{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Pandas\n", "\n", "**[Pandas](https://pandas.pydata.org/)** is an open-source, BSD-licensed Python library. Pandas is a handy and useful data-structure tool for analyzing large and complex data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial we will practice: \n", "\n", "* DataFrame, \n", "* Data Selection, \n", "* Group-By, \n", "* Series, \n", "* Sorting, \n", "* Searching, \n", "* statistics. \n", "\n", "Let us practice Data analysis using Pandas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise, we are using **[automobile_data.csv](/content/tools/pandas/automobile_data.csv)** for data analysis. This Dataset has different characteristics of an auto such as body-style, wheel-base, engine-type, price, mileage, horsepower, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**What included in this Pandas exercise?**\n", "\n", "It contains 10 questions. The solution is provided for each question.\n", "Each question includes a specific Pandas topic you need to learn.\n", "When you complete each question, you get more familiar with data analysis using pandas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 1: From the given dataset print the first and last five rows" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:35.366922Z", "start_time": "2021-06-17T11:32:34.742909Z" } }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
indexcompanybody-stylewheel-baselengthengine-typenum-of-cylindershorsepoweraverage-mileageprice
00alfa-romeroconvertible88.6168.8dohcfour1112113495.0
11alfa-romeroconvertible88.6168.8dohcfour1112116500.0
22alfa-romerohatchback94.5171.2ohcvsix1541916500.0
33audisedan99.8176.6ohcfour1022413950.0
44audisedan99.4176.6ohcfive1151817450.0
\n", "
" ], "text/plain": [ " index company body-style wheel-base length engine-type \\\n", "0 0 alfa-romero convertible 88.6 168.8 dohc \n", "1 1 alfa-romero convertible 88.6 168.8 dohc \n", "2 2 alfa-romero hatchback 94.5 171.2 ohcv \n", "3 3 audi sedan 99.8 176.6 ohc \n", "4 4 audi sedan 99.4 176.6 ohc \n", "\n", " num-of-cylinders horsepower average-mileage price \n", "0 four 111 21 13495.0 \n", "1 four 111 21 16500.0 \n", "2 six 154 19 16500.0 \n", "3 four 102 24 13950.0 \n", "4 five 115 18 17450.0 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "#df = pd.read_csv(\"C:\\\\Users\\\\Milaan\\\\01_Learn_Python4Data\\\\10_Python_Pandas_Module\\\\automobile_data.csv\")\n", "df = pd.read_csv(\"automobile_data.csv\") # just give the name of file only if the file is in the same folder.\n", "df.head(5)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:37.551466Z", "start_time": "2021-06-17T11:32:37.510453Z" }, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
indexcompanybody-stylewheel-baselengthengine-typenum-of-cylindershorsepoweraverage-mileageprice
5681volkswagensedan97.3171.7ohcfour85277975.0
5782volkswagensedan97.3171.7ohcfour52377995.0
5886volkswagensedan97.3171.7ohcfour100269995.0
5987volvosedan104.3188.8ohcfour1142312940.0
6088volvowagon104.3188.8ohcfour1142313415.0
\n", "
" ], "text/plain": [ " index company body-style wheel-base length engine-type \\\n", "56 81 volkswagen sedan 97.3 171.7 ohc \n", "57 82 volkswagen sedan 97.3 171.7 ohc \n", "58 86 volkswagen sedan 97.3 171.7 ohc \n", "59 87 volvo sedan 104.3 188.8 ohc \n", "60 88 volvo wagon 104.3 188.8 ohc \n", "\n", " num-of-cylinders horsepower average-mileage price \n", "56 four 85 27 7975.0 \n", "57 four 52 37 7995.0 \n", "58 four 100 26 9995.0 \n", "59 four 114 23 12940.0 \n", "60 four 114 23 13415.0 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv(\"automobile_data.csv\")\n", "df.tail(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 2: Clean the dataset and update the CSV file\n", "\n", "**Hint:** Replace all column values which contain **`?`**, **`n.a`**, or **`NaN`**." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:39.765313Z", "start_time": "2021-06-17T11:32:39.733085Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " index company body-style wheel-base length engine-type \\\n", "0 0 alfa-romero convertible 88.6 168.8 dohc \n", "1 1 alfa-romero convertible 88.6 168.8 dohc \n", "2 2 alfa-romero hatchback 94.5 171.2 ohcv \n", "3 3 audi sedan 99.8 176.6 ohc \n", "4 4 audi sedan 99.4 176.6 ohc \n", ".. ... ... ... ... ... ... \n", "56 81 volkswagen sedan 97.3 171.7 ohc \n", "57 82 volkswagen sedan 97.3 171.7 ohc \n", "58 86 volkswagen sedan 97.3 171.7 ohc \n", "59 87 volvo sedan 104.3 188.8 ohc \n", "60 88 volvo wagon 104.3 188.8 ohc \n", "\n", " num-of-cylinders horsepower average-mileage price \n", "0 four 111 21 13495.0 \n", "1 four 111 21 16500.0 \n", "2 six 154 19 16500.0 \n", "3 four 102 24 13950.0 \n", "4 five 115 18 17450.0 \n", ".. ... ... ... ... \n", "56 four 85 27 7975.0 \n", "57 four 52 37 7995.0 \n", "58 four 100 26 9995.0 \n", "59 four 114 23 12940.0 \n", "60 four 114 23 13415.0 \n", "\n", "[61 rows x 10 columns]\n" ] } ], "source": [ "#df = pd.read_csv(\"C:\\\\Users\\\\Milaan\\\\01_Learn_Python4Data\\\\10_Python_Pandas_Module\\\\automobile_data.csv\", na_values={\n", "df = pd.read_csv(\"automobile_data.csv\", na_values={\n", "'price':[\"?\",\"n.a\"],\n", "'stroke':[\"?\",\"n.a\"],\n", "'horsepower':[\"?\",\"n.a\"],\n", "'peak-rpm':[\"?\",\"n.a\"],\n", "'average-mileage':[\"?\",\"n.a\"]})\n", "print (df)\n", "\n", "\n", "#df.to_csv(\"C:\\\\Users\\\\Milaan\\\\01_Learn_Python4Data\\\\10_Python_Pandas_Module\\\\automobile_data1.csv\")\n", "df.to_csv(\"automobile_data1.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 3: Find the most expensive car company name\n", "\n", "Print most expensive car’s company name and price." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:42.119287Z", "start_time": "2021-06-17T11:32:42.077297Z" }, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
companyprice
35mercedes-benz45400.0
\n", "
" ], "text/plain": [ " company price\n", "35 mercedes-benz 45400.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "df = pd.read_csv(\"automobile_data1.csv\")\n", "df = df [['company','price']][df.price==df['price'].max()]\n", "df\n", "\n", "#or\n", "\n", "#df=df.groupby([\"company\"]).sum().sort_values(\"price\", ascending=False)\n", "#df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 4: Print All Toyota Cars details" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:43.901490Z", "start_time": "2021-06-17T11:32:43.813114Z" } }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0indexcompanybody-stylewheel-baselengthengine-typenum-of-cylindershorsepoweraverage-mileageprice
484866toyotahatchback95.7158.7ohcfour62355348.0
494967toyotahatchback95.7158.7ohcfour62316338.0
505068toyotahatchback95.7158.7ohcfour62316488.0
515169toyotawagon95.7169.7ohcfour62316918.0
525270toyotawagon95.7169.7ohcfour62277898.0
535371toyotawagon95.7169.7ohcfour62278778.0
545479toyotawagon104.5187.8dohcsix1561915750.0
\n", "
" ], "text/plain": [ " Unnamed: 0 index company body-style wheel-base length engine-type \\\n", "48 48 66 toyota hatchback 95.7 158.7 ohc \n", "49 49 67 toyota hatchback 95.7 158.7 ohc \n", "50 50 68 toyota hatchback 95.7 158.7 ohc \n", "51 51 69 toyota wagon 95.7 169.7 ohc \n", "52 52 70 toyota wagon 95.7 169.7 ohc \n", "53 53 71 toyota wagon 95.7 169.7 ohc \n", "54 54 79 toyota wagon 104.5 187.8 dohc \n", "\n", " num-of-cylinders horsepower average-mileage price \n", "48 four 62 35 5348.0 \n", "49 four 62 31 6338.0 \n", "50 four 62 31 6488.0 \n", "51 four 62 31 6918.0 \n", "52 four 62 27 7898.0 \n", "53 four 62 27 8778.0 \n", "54 six 156 19 15750.0 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv(\"automobile_data1.csv\")\n", "car_Manufacturers = df.groupby('company')\n", "toyotaDf = car_Manufacturers.get_group('toyota')\n", "toyotaDf\n", "\n", "#or\n", "\n", "#df=pd.DataFrame(pd.read_csv(r'C:\\\\Users\\\\Milaan\\\\01_Learn_Python4Data\\\\10_Python_Pandas_Module\\\\automobile_data1.csv'))\n", "#df[(df.company == 'toyota')]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5: Count total cars per company" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:55.486312Z", "start_time": "2021-06-17T11:32:55.462878Z" }, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "toyota 7\n", "bmw 6\n", "nissan 5\n", "mazda 5\n", "volkswagen 4\n", "audi 4\n", "mitsubishi 4\n", "mercedes-benz 4\n", "isuzu 3\n", "alfa-romero 3\n", "honda 3\n", "jaguar 3\n", "chevrolet 3\n", "porsche 3\n", "volvo 2\n", "dodge 2\n", "Name: company, dtype: int64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv(\"automobile_data1.csv\")\n", "df['company'].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 6: Find each company’s Higesht price car" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:32:58.961855Z", "start_time": "2021-06-17T11:32:58.854438Z" }, "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":5: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.\n", " priceDf = car_Manufacturers['company','price'].max()\n" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
companyprice
company
alfa-romeroalfa-romero16500.0
audiaudi18920.0
bmwbmw41315.0
chevroletchevrolet6575.0
dodgedodge6377.0
hondahonda12945.0
isuzuisuzu6785.0
jaguarjaguar36000.0
mazdamazda18344.0
mercedes-benzmercedes-benz45400.0
mitsubishimitsubishi8189.0
nissannissan13499.0
porscheporsche37028.0
toyotatoyota15750.0
volkswagenvolkswagen9995.0
volvovolvo13415.0
\n", "
" ], "text/plain": [ " company price\n", "company \n", "alfa-romero alfa-romero 16500.0\n", "audi audi 18920.0\n", "bmw bmw 41315.0\n", "chevrolet chevrolet 6575.0\n", "dodge dodge 6377.0\n", "honda honda 12945.0\n", "isuzu isuzu 6785.0\n", "jaguar jaguar 36000.0\n", "mazda mazda 18344.0\n", "mercedes-benz mercedes-benz 45400.0\n", "mitsubishi mitsubishi 8189.0\n", "nissan nissan 13499.0\n", "porsche porsche 37028.0\n", "toyota toyota 15750.0\n", "volkswagen volkswagen 9995.0\n", "volvo volvo 13415.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv(\"automobile_data1.csv\")\n", "car_Manufacturers = df.groupby('company')\n", "priceDf = car_Manufacturers['company','price'].max()\n", "priceDf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 7: Find the average mileage of each car making company" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:33:09.006171Z", "start_time": "2021-06-17T11:33:08.975899Z" }, "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":5: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.\n", " mileageDf = car_Manufacturers['company','average-mileage'].mean()\n" ] }, { "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", "
average-mileage
company
alfa-romero20.333333
audi20.000000
bmw19.000000
chevrolet41.000000
dodge31.000000
honda26.333333
isuzu33.333333
jaguar14.333333
mazda28.000000
mercedes-benz18.000000
mitsubishi29.500000
nissan31.400000
porsche17.000000
toyota28.714286
volkswagen31.750000
volvo23.000000
\n", "
" ], "text/plain": [ " average-mileage\n", "company \n", "alfa-romero 20.333333\n", "audi 20.000000\n", "bmw 19.000000\n", "chevrolet 41.000000\n", "dodge 31.000000\n", "honda 26.333333\n", "isuzu 33.333333\n", "jaguar 14.333333\n", "mazda 28.000000\n", "mercedes-benz 18.000000\n", "mitsubishi 29.500000\n", "nissan 31.400000\n", "porsche 17.000000\n", "toyota 28.714286\n", "volkswagen 31.750000\n", "volvo 23.000000" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_csv(\"automobile_data1.csv\")\n", "car_Manufacturers = df.groupby('company')\n", "mileageDf = car_Manufacturers['company','average-mileage'].mean()\n", "mileageDf\n", "\n", "\n", "#or\n", "\n", "#company_grp=df.groupby('company')\n", "#company_grp.agg({\n", "# 'average-mileage':np.mean\n", "#})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 8: Sort all cars by Price column" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:33:16.908908Z", "start_time": "2021-06-17T11:33:16.822974Z" }, "scrolled": true }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0indexcompanybody-stylewheel-baselengthengine-typenum-of-cylindershorsepoweraverage-mileageprice
353547mercedes-benzhardtop112.0199.2ohcveight1841445400.0
111114bmwsedan103.5193.8ohcsix1821641315.0
343446mercedes-benzsedan120.9208.1ohcveight1841440960.0
464662porscheconvertible89.5168.9ohcfsix2071737028.0
121215bmwsedan110.0197.0ohcsix1821536880.0
\n", "
" ], "text/plain": [ " Unnamed: 0 index company body-style wheel-base length \\\n", "35 35 47 mercedes-benz hardtop 112.0 199.2 \n", "11 11 14 bmw sedan 103.5 193.8 \n", "34 34 46 mercedes-benz sedan 120.9 208.1 \n", "46 46 62 porsche convertible 89.5 168.9 \n", "12 12 15 bmw sedan 110.0 197.0 \n", "\n", " engine-type num-of-cylinders horsepower average-mileage price \n", "35 ohcv eight 184 14 45400.0 \n", "11 ohc six 182 16 41315.0 \n", "34 ohcv eight 184 14 40960.0 \n", "46 ohcf six 207 17 37028.0 \n", "12 ohc six 182 15 36880.0 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "carsDf = pd.read_csv(\"automobile_data1.csv\")\n", "carsDf = carsDf.sort_values(by=['price', 'horsepower'], ascending=False)\n", "carsDf.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 9: Concatenate two data frames using the following conditions\n", "\n", "**Hint:** Create two data frames using the following two **[dictionary](http://localhost:8888/notebooks/01_Learn_Python4Data/02_Python_Datatypes/005_Python_Dictionary.ipynb)**." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:33:18.682321Z", "start_time": "2021-06-17T11:33:18.647168Z" }, "scrolled": true }, "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", "
CompanyPrice
Germany0Ford23845
1Mercedes171995
2BMV135925
3Audi71400
Japan0Toyota29995
1Honda23600
2Nissan61500
3Mitsubishi58900
\n", "
" ], "text/plain": [ " Company Price\n", "Germany 0 Ford 23845\n", " 1 Mercedes 171995\n", " 2 BMV 135925\n", " 3 Audi 71400\n", "Japan 0 Toyota 29995\n", " 1 Honda 23600\n", " 2 Nissan 61500\n", " 3 Mitsubishi 58900" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "GermanCars = {'Company': ['Ford', 'Mercedes', 'BMV', 'Audi'], 'Price': [23845, 171995, 135925 , 71400]}\n", "carsDf1 = pd.DataFrame.from_dict(GermanCars)\n", "\n", "japaneseCars = {'Company': ['Toyota', 'Honda', 'Nissan', 'Mitsubishi '], 'Price': [29995, 23600, 61500 , 58900]}\n", "carsDf2 = pd.DataFrame.from_dict(japaneseCars)\n", "\n", "carsDf = pd.concat([carsDf1, carsDf2], keys=[\"Germany\", \"Japan\"])\n", "carsDf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 10: Merge two data frames using the following condition\n", "\n", "**Hint:** Create two data frames using the following two Dicts, Merge two data frames, and append the second data frame as a new column to the first data frame." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2021-06-17T11:33:26.107039Z", "start_time": "2021-06-17T11:33:26.044054Z" } }, "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", "
CompanyPricehorsepower
0Toyota23845141
1Honda1799580
2BMV135925182
3Audi71400160
\n", "
" ], "text/plain": [ " Company Price horsepower\n", "0 Toyota 23845 141\n", "1 Honda 17995 80\n", "2 BMV 135925 182\n", "3 Audi 71400 160" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "Car_Price = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'Price': [23845, 17995, 135925 , 71400]}\n", "carPriceDf = pd.DataFrame.from_dict(Car_Price)\n", "\n", "car_Horsepower = {'Company': ['Toyota', 'Honda', 'BMV', 'Audi'], 'horsepower': [141, 80, 182 , 160]}\n", "carsHorsepowerDf = pd.DataFrame.from_dict(car_Horsepower)\n", "\n", "carsDf = pd.merge(carPriceDf, carsHorsepowerDf, on=\"Company\")\n", "carsDf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "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.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }