Skip to content

Instantly share code, notes, and snippets.

@Anugithub2019
Created March 8, 2021 22:42
Show Gist options
  • Save Anugithub2019/289e335b2ec6190661c75be65a91d6be to your computer and use it in GitHub Desktop.
Save Anugithub2019/289e335b2ec6190661c75be65a91d6be to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "<center>\n <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n</center>\n\n# Introduction to Pandas in Python\n\nEstimated time needed: **15** minutes\n\n## Objectives\n\nAfter completing this lab you will be able to:\n\n- Use Pandas to access and view data\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<h2>Table of Contents</h2>\n<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n <ul>\n <li><a href=\"dataset\">About the Dataset</a></li>\n <li><a href=\"pandas\">Introduction of <code>Pandas</code></a></li>\n <li><a href=\"data\">Viewing Data and Accessing Data</a></li>\n <li><a href=\"quiz\">Quiz on DataFrame</a></li>\n </ul>\n \n</div>\n\n<hr>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<h2 id=\"dataset\">About the Dataset</h2>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The table has one row for each album and several columns\n\n<ul>\n <li><b>artist</b>: Name of the artist</li>\n <li><b>album</b>: Name of the album</li>\n <li><b>released_year</b>: Year the album was released</li>\n <li><b>length_min_sec</b>: Length of the album (hours,minutes,seconds)</li>\n <li><b>genre</b>: Genre of the album</li>\n <li><b>music_recording_sales_millions</b>: Music recording sales (millions in USD) on <a href=\"http://www.song-database.com/\">[SONG://DATABASE]</a></li>\n <li><b>claimed_sales_millions</b>: Album's claimed sales (millions in USD) on <a href=\"http://www.song-database.com/\">[SONG://DATABASE]</a></li>\n <li><b>date_released</b>: Date on which the album was released</li>\n <li><b>soundtrack</b>: Indicates if the album is the movie soundtrack (Y) or (N)</li>\n <li><b>rating_of_friends</b>: Indicates the rating from your friends from 1 to 10</li>\n</ul>\n\nYou can see the dataset here:\n\n<font size=\"1\">\n<table font-size:xx-small>\n <tr>\n <th>Artist</th>\n <th>Album</th> \n <th>Released</th>\n <th>Length</th>\n <th>Genre</th> \n <th>Music recording sales (millions)</th>\n <th>Claimed sales (millions)</th>\n <th>Released</th>\n <th>Soundtrack</th>\n <th>Rating (friends)</th>\n </tr>\n <tr>\n <td>Michael Jackson</td>\n <td>Thriller</td> \n <td>1982</td>\n <td>00:42:19</td>\n <td>Pop, rock, R&B</td>\n <td>46</td>\n <td>65</td>\n <td>30-Nov-82</td>\n <td></td>\n <td>10.0</td>\n </tr>\n <tr>\n <td>AC/DC</td>\n <td>Back in Black</td> \n <td>1980</td>\n <td>00:42:11</td>\n <td>Hard rock</td>\n <td>26.1</td>\n <td>50</td>\n <td>25-Jul-80</td>\n <td></td>\n <td>8.5</td>\n </tr>\n <tr>\n <td>Pink Floyd</td>\n <td>The Dark Side of the Moon</td> \n <td>1973</td>\n <td>00:42:49</td>\n <td>Progressive rock</td>\n <td>24.2</td>\n <td>45</td>\n <td>01-Mar-73</td>\n <td></td>\n <td>9.5</td>\n </tr>\n <tr>\n <td>Whitney Houston</td>\n <td>The Bodyguard</td> \n <td>1992</td>\n <td>00:57:44</td>\n <td>Soundtrack/R&B, soul, pop</td>\n <td>26.1</td>\n <td>50</td>\n <td>25-Jul-80</td>\n <td>Y</td>\n <td>7.0</td>\n </tr>\n <tr>\n <td>Meat Loaf</td>\n <td>Bat Out of Hell</td> \n <td>1977</td>\n <td>00:46:33</td>\n <td>Hard rock, progressive rock</td>\n <td>20.6</td>\n <td>43</td>\n <td>21-Oct-77</td>\n <td></td>\n <td>7.0</td>\n </tr>\n <tr>\n <td>Eagles</td>\n <td>Their Greatest Hits (1971-1975)</td> \n <td>1976</td>\n <td>00:43:08</td>\n <td>Rock, soft rock, folk rock</td>\n <td>32.2</td>\n <td>42</td>\n <td>17-Feb-76</td>\n <td></td>\n <td>9.5</td>\n </tr>\n <tr>\n <td>Bee Gees</td>\n <td>Saturday Night Fever</td> \n <td>1977</td>\n <td>1:15:54</td>\n <td>Disco</td>\n <td>20.6</td>\n <td>40</td>\n <td>15-Nov-77</td>\n <td>Y</td>\n <td>9.0</td>\n </tr>\n <tr>\n <td>Fleetwood Mac</td>\n <td>Rumours</td> \n <td>1977</td>\n <td>00:40:01</td>\n <td>Soft rock</td>\n <td>27.9</td>\n <td>40</td>\n <td>04-Feb-77</td>\n <td></td>\n <td>9.5</td>\n </tr>\n</table></font>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<hr>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<h2 id=\"pandas\">Introduction of <code>Pandas</code></h2>\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Dependency needed to install file \n\n!pip install xlrd",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Requirement already satisfied: xlrd in /opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages (1.2.0)\r\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "code",
"source": "# Import required library\n\nimport pandas as pd",
"execution_count": 2,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "After the import command, we now have access to a large number of pre-built classes and functions. This assumes the library is installed; in our lab environment all the necessary libraries are installed. One way pandas allows you to work with data is a dataframe. Let's go through the process to go from a comma separated values (<b>.csv</b>) file to a dataframe. This variable <code>csv_path</code> stores the path of the <b>.csv</b>, that is used as an argument to the <code>read_csv</code> function. The result is stored in the object <code>df</code>, this is a common short form used for a variable referring to a Pandas dataframe. \n"
},
{
"metadata": {
"scrolled": true
},
"cell_type": "code",
"source": "# Read data from CSV file\n\ncsv_path = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/data/TopSellingAlbums.csv'\ndf = pd.read_csv(csv_path)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "We can use the method <code>head()</code> to examine the first five rows of a dataframe: \n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Print first five rows of the dataframe\n\ndf.head()",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": " Artist Album Released Length \\\n0 Michael Jackson Thriller 1982 0:42:19 \n1 AC/DC Back in Black 1980 0:42:11 \n2 Pink Floyd The Dark Side of the Moon 1973 0:42:49 \n3 Whitney Houston The Bodyguard 1992 0:57:44 \n4 Meat Loaf Bat Out of Hell 1977 0:46:33 \n\n Genre Music Recording Sales (millions) \\\n0 pop, rock, R&B 46.0 \n1 hard rock 26.1 \n2 progressive rock 24.2 \n3 R&B, soul, pop 27.4 \n4 hard rock, progressive rock 20.6 \n\n Claimed Sales (millions) Released.1 Soundtrack Rating \n0 65 30-Nov-82 NaN 10.0 \n1 50 25-Jul-80 NaN 9.5 \n2 45 01-Mar-73 NaN 9.0 \n3 44 17-Nov-92 Y 8.5 \n4 43 21-Oct-77 NaN 8.0 ",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Artist</th>\n <th>Album</th>\n <th>Released</th>\n <th>Length</th>\n <th>Genre</th>\n <th>Music Recording Sales (millions)</th>\n <th>Claimed Sales (millions)</th>\n <th>Released.1</th>\n <th>Soundtrack</th>\n <th>Rating</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Michael Jackson</td>\n <td>Thriller</td>\n <td>1982</td>\n <td>0:42:19</td>\n <td>pop, rock, R&amp;B</td>\n <td>46.0</td>\n <td>65</td>\n <td>30-Nov-82</td>\n <td>NaN</td>\n <td>10.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>AC/DC</td>\n <td>Back in Black</td>\n <td>1980</td>\n <td>0:42:11</td>\n <td>hard rock</td>\n <td>26.1</td>\n <td>50</td>\n <td>25-Jul-80</td>\n <td>NaN</td>\n <td>9.5</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Pink Floyd</td>\n <td>The Dark Side of the Moon</td>\n <td>1973</td>\n <td>0:42:49</td>\n <td>progressive rock</td>\n <td>24.2</td>\n <td>45</td>\n <td>01-Mar-73</td>\n <td>NaN</td>\n <td>9.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Whitney Houston</td>\n <td>The Bodyguard</td>\n <td>1992</td>\n <td>0:57:44</td>\n <td>R&amp;B, soul, pop</td>\n <td>27.4</td>\n <td>44</td>\n <td>17-Nov-92</td>\n <td>Y</td>\n <td>8.5</td>\n </tr>\n <tr>\n <th>4</th>\n <td>Meat Loaf</td>\n <td>Bat Out of Hell</td>\n <td>1977</td>\n <td>0:46:33</td>\n <td>hard rock, progressive rock</td>\n <td>20.6</td>\n <td>43</td>\n <td>21-Oct-77</td>\n <td>NaN</td>\n <td>8.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": " We use the path of the excel file and the function <code>read_excel</code>. The result is a data frame as before:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Read data from Excel File and print the first five rows\n\nxlsx_path = 'https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%204/Datasets/TopSellingAlbums.xlsx'\n\ndf = pd.read_excel(xlsx_path)\ndf.head()",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": " Artist Album Released Length \\\n0 Michael Jackson Thriller 1982 00:42:19 \n1 AC/DC Back in Black 1980 00:42:11 \n2 Pink Floyd The Dark Side of the Moon 1973 00:42:49 \n3 Whitney Houston The Bodyguard 1992 00:57:44 \n4 Meat Loaf Bat Out of Hell 1977 00:46:33 \n\n Genre Music Recording Sales (millions) \\\n0 pop, rock, R&B 46.0 \n1 hard rock 26.1 \n2 progressive rock 24.2 \n3 R&B, soul, pop 27.4 \n4 hard rock, progressive rock 20.6 \n\n Claimed Sales (millions) Released.1 Soundtrack Rating \n0 65 1982-11-30 NaN 10.0 \n1 50 1980-07-25 NaN 9.5 \n2 45 1973-03-01 NaN 9.0 \n3 44 1992-11-17 Y 8.5 \n4 43 1977-10-21 NaN 8.0 ",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Artist</th>\n <th>Album</th>\n <th>Released</th>\n <th>Length</th>\n <th>Genre</th>\n <th>Music Recording Sales (millions)</th>\n <th>Claimed Sales (millions)</th>\n <th>Released.1</th>\n <th>Soundtrack</th>\n <th>Rating</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>Michael Jackson</td>\n <td>Thriller</td>\n <td>1982</td>\n <td>00:42:19</td>\n <td>pop, rock, R&amp;B</td>\n <td>46.0</td>\n <td>65</td>\n <td>1982-11-30</td>\n <td>NaN</td>\n <td>10.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>AC/DC</td>\n <td>Back in Black</td>\n <td>1980</td>\n <td>00:42:11</td>\n <td>hard rock</td>\n <td>26.1</td>\n <td>50</td>\n <td>1980-07-25</td>\n <td>NaN</td>\n <td>9.5</td>\n </tr>\n <tr>\n <th>2</th>\n <td>Pink Floyd</td>\n <td>The Dark Side of the Moon</td>\n <td>1973</td>\n <td>00:42:49</td>\n <td>progressive rock</td>\n <td>24.2</td>\n <td>45</td>\n <td>1973-03-01</td>\n <td>NaN</td>\n <td>9.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>Whitney Houston</td>\n <td>The Bodyguard</td>\n <td>1992</td>\n <td>00:57:44</td>\n <td>R&amp;B, soul, pop</td>\n <td>27.4</td>\n <td>44</td>\n <td>1992-11-17</td>\n <td>Y</td>\n <td>8.5</td>\n </tr>\n <tr>\n <th>4</th>\n <td>Meat Loaf</td>\n <td>Bat Out of Hell</td>\n <td>1977</td>\n <td>00:46:33</td>\n <td>hard rock, progressive rock</td>\n <td>20.6</td>\n <td>43</td>\n <td>1977-10-21</td>\n <td>NaN</td>\n <td>8.0</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "We can access the column <b>Length</b> and assign it a new dataframe <b>x</b>:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access to the column Length\n\nx = df[['Length']]\nx",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": " Length\n0 00:42:19\n1 00:42:11\n2 00:42:49\n3 00:57:44\n4 00:46:33\n5 00:43:08\n6 01:15:54\n7 00:40:01",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Length</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>00:42:19</td>\n </tr>\n <tr>\n <th>1</th>\n <td>00:42:11</td>\n </tr>\n <tr>\n <th>2</th>\n <td>00:42:49</td>\n </tr>\n <tr>\n <th>3</th>\n <td>00:57:44</td>\n </tr>\n <tr>\n <th>4</th>\n <td>00:46:33</td>\n </tr>\n <tr>\n <th>5</th>\n <td>00:43:08</td>\n </tr>\n <tr>\n <th>6</th>\n <td>01:15:54</td>\n </tr>\n <tr>\n <th>7</th>\n <td>00:40:01</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": " The process is shown in the figure: \n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%204/Images/DataEgOne.png\" width=\"750\" />\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<hr>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<h2 id=\"data\">Viewing Data and Accessing Data</h2>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can also get a column as a series. You can think of a Pandas series as a 1-D dataframe. Just use one bracket: \n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Get the column as a series\n\nx = df['Length']\nx",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "0 00:42:19\n1 00:42:11\n2 00:42:49\n3 00:57:44\n4 00:46:33\n5 00:43:08\n6 01:15:54\n7 00:40:01\nName: Length, dtype: object"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can also get a column as a dataframe. For example, we can assign the column <b>Artist</b>:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Get the column as a dataframe\n\nx = type(df[['Artist']])\nx",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "pandas.core.frame.DataFrame"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can do the same thing for multiple columns; we just put the dataframe name, in this case, <code>df</code>, and the name of the multiple column headers enclosed in double brackets. The result is a new dataframe comprised of the specified columns:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access to multiple columns\n\ny = df[['Artist','Length','Genre']]\ny",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The process is shown in the figure:\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<img src = \"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Chapter%204/Images/DataEgTwo.png\" width=\"1100\" />\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "One way to access unique elements is the <code>iloc</code> method, where you can access the 1st row and the 1st column as follows:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the value on the first row and the first column\n\ndf.iloc[0, 0]",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "'Michael Jackson'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can access the 2nd row and the 1st column as follows:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the value on the second row and the first column\n\ndf.iloc[1,0]",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "'AC/DC'"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can access the 1st row and the 3rd column as follows: \n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the value on the first row and the third column\n\ndf.iloc[0,2]",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "1982"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the value on the second row and the third column\ndf.iloc[1,2]",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "1980"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "This is shown in the following image\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/images/2_loc.PNG\" width=\"750\" />\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can access the column using the name as well, the following are the same as above: \n"
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the column using the name\n\ndf.loc[1, 'Artist']",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the column using the name\n\ndf.loc[1, 'Artist']",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the column using the name\n\ndf.loc[0, 'Released']",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "code",
"source": "# Access the column using the name\n\ndf.loc[1, 'Released']",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/images/1_loc.png\" width=\"750\" />\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "You can perform slicing using both the index and the name of the column:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Slicing the dataframe\n\ndf.iloc[0:2, 0:3]",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/images/4_loc.png\" width=\"750\" />\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Slicing the dataframe using name\n\ndf.loc[0:2, 'Artist':'Released']",
"execution_count": null,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/images/3_loc.png\" width=\"750\" />\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<hr>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<h2 id=\"quiz\">Quiz on DataFrame</h2>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Use a variable <code>q</code> to store the column <b>Rating</b> as a dataframe\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Write your code below and press Shift+Enter to execute\nq = df[['Rating']]\nq",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": " Rating\n0 10.0\n1 9.5\n2 9.0\n3 8.5\n4 8.0\n5 7.5\n6 7.0\n7 6.5",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Rating</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>10.0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>9.5</td>\n </tr>\n <tr>\n <th>2</th>\n <td>9.0</td>\n </tr>\n <tr>\n <th>3</th>\n <td>8.5</td>\n </tr>\n <tr>\n <th>4</th>\n <td>8.0</td>\n </tr>\n <tr>\n <th>5</th>\n <td>7.5</td>\n </tr>\n <tr>\n <th>6</th>\n <td>7.0</td>\n </tr>\n <tr>\n <th>7</th>\n <td>6.5</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nq = df[['Rating']]\nq\n \n```\n\n</details>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Assign the variable <code>q</code> to the dataframe that is made up of the column <b>Released</b> and <b>Artist</b>:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Write your code below and press Shift+Enter to execute\nq = df[['Released' , 'Artist']]\nq",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": " Released Artist\n0 1982 Michael Jackson\n1 1980 AC/DC\n2 1973 Pink Floyd\n3 1992 Whitney Houston\n4 1977 Meat Loaf\n5 1976 Eagles\n6 1977 Bee Gees\n7 1977 Fleetwood Mac",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Released</th>\n <th>Artist</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>1982</td>\n <td>Michael Jackson</td>\n </tr>\n <tr>\n <th>1</th>\n <td>1980</td>\n <td>AC/DC</td>\n </tr>\n <tr>\n <th>2</th>\n <td>1973</td>\n <td>Pink Floyd</td>\n </tr>\n <tr>\n <th>3</th>\n <td>1992</td>\n <td>Whitney Houston</td>\n </tr>\n <tr>\n <th>4</th>\n <td>1977</td>\n <td>Meat Loaf</td>\n </tr>\n <tr>\n <th>5</th>\n <td>1976</td>\n <td>Eagles</td>\n </tr>\n <tr>\n <th>6</th>\n <td>1977</td>\n <td>Bee Gees</td>\n </tr>\n <tr>\n <th>7</th>\n <td>1977</td>\n <td>Fleetwood Mac</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<details><summary>Click here for the solution</summary>\n\n```python\nq = df[['Released', 'Artist']]\nq\n \n```\n\n</details>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Access the 2nd row and the 3rd column of <code>df</code>:\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "# Write your code below and press Shift+Enter to execute\ndf.iloc[1,2]",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "1980"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<details><summary>Click here for the solution</summary>\n\n```python\ndf.iloc[1, 2]\n \n```\n\n</details>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Use the following list to convert the dataframe index <code>df</code> to characters and assign it to <code>df_new</code>; find the element corresponding to the row index <code>a</code> and column <code>'Artist'</code>. Then select the rows <code>a</code> through <code>d</code> for the column <code>'Artist'</code>\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "new_index=['a','b','c','d','e','f','g','h']\n\ndf_new = df\ndf_new.index = new_index\ndf_new.loc['a', 'Artist']\ndf_new.loc['a':'d', 'Artist']",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "a Michael Jackson\nb AC/DC\nc Pink Floyd\nd Whitney Houston\nName: Artist, dtype: object"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<details><summary>Click here for the solution</summary>\n\n```python\ndf_new=df\ndf_new.index=new_index\ndf_new.loc['a', 'Artist']\ndf_new.loc['a':'d', 'Artist']\n \n```\n\n</details>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<hr>\n<h2>The last exercise!</h2>\n<p>Congratulations, you have completed your first lesson and hands-on lab in Python. However, there is one more thing you need to do. The Data Science community encourages sharing work. The best way to share and showcase your work is to share it on GitHub. By sharing your notebook on GitHub you are not only building your reputation with fellow data scientists, but you can also show it off when applying for a job. Even though this was your first piece of work, it is never too early to start building good habits. So, please read and follow <a href=\"https://cognitiveclass.ai/blog/data-scientists-stand-out-by-sharing-your-notebooks/\" target=\"_blank\">this article</a> to learn how to share your work.\n<hr>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n<h2>Get IBM Watson Studio free of charge!</h2>\n <p><a href=\"https://cloud.ibm.com/catalog/services/watson-studio\"><img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/BottomAd.png\" width=\"750\" align=\"center\"></a></p>\n</div>\n"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Authors:\n\n [Joseph Santarcangelo](https://www.linkedin.com/in/joseph-s-50398b136?cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ) \n\nJoseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n\n## Change Log\n\n| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n| ----------------- | ------- | ---------- | ---------------------------------- |\n| 2020-08-26 | 2.0 | Lavanya | Moved lab to course repo in GitLab |\n| 2020-11-24 | 3.0 | Nayef | Added new images |\n| | | | |\n\n<hr/>\n\n## <h3 align=\"center\"> \u00a9 IBM Corporation 2020. All rights reserved. <h3/>\n"
},
{
"metadata": {},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.10",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment