Skip to content

Instantly share code, notes, and snippets.

@MohdAzamSayeed
Last active November 12, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohdAzamSayeed/9f29ad890d1afae0444fc431a12ced3e to your computer and use it in GitHub Desktop.
Save MohdAzamSayeed/9f29ad890d1afae0444fc431a12ced3e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.\tReplace Not available with NaN value in pandas? (Example.csv)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>class</th>\n",
" <th>total marks</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Mukul</td>\n",
" <td>12</td>\n",
" <td>454.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Rohan</td>\n",
" <td>12</td>\n",
" <td>433.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Shivam</td>\n",
" <td>11</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Ragav</td>\n",
" <td>11</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Monu</td>\n",
" <td>10</td>\n",
" <td>456.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name class total marks\n",
"0 Mukul 12 454.0\n",
"1 Rohan 12 433.0\n",
"2 Shivam 11 NaN\n",
"3 Ragav 11 NaN\n",
"4 Monu 10 456.0"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"df=pd.read_csv('Example.csv',na_values=['not available'])\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2.\tTo get the first 3 rows of a given DataFrame.\n",
"Sample Python dictionary data and list labels:\n",
"exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],\n",
"'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],\n",
"'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n",
"'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}\n",
"labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': ['Mukul', 'Rohan', 'Shivam'], 'class': [12, 12, 11]}"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dict_data = {'name': list(df.loc[:2,'name']),'class': list(df.loc[:2,'class'])}\n",
"dict_data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>e</th>\n",
" <td>Emily</td>\n",
" <td>9.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>f</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>g</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>h</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>i</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>j</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"a Anastasia 12.5 1 yes\n",
"b Dima 9.0 3 no\n",
"c Katherine 16.5 2 yes\n",
"d James NaN 3 no\n",
"e Emily 9.0 2 no\n",
"f Michael 20.0 3 yes\n",
"g Matthew 14.5 1 yes\n",
"h Laura NaN 1 no\n",
"i Kevin 8.0 2 no\n",
"j Jonas 19.0 1 yes"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"dictx= {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'], 'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19], 'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1], 'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}\n",
"df2=pd.DataFrame(dictx,index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'])\n",
"df2"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"a Anastasia 12.5 1 yes\n",
"b Dima 9.0 3 no\n",
"c Katherine 16.5 2 yes"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.iloc[:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3.\tTo select the 'name' and 'score' columns from frame"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>e</th>\n",
" <td>Emily</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>f</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>g</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>h</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>i</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>j</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score\n",
"a Anastasia 12.5\n",
"b Dima 9.0\n",
"c Katherine 16.5\n",
"d James NaN\n",
"e Emily 9.0\n",
"f Michael 20.0\n",
"g Matthew 14.5\n",
"h Laura NaN\n",
"i Kevin 8.0\n",
"j Jonas 19.0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[:,['name','score']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4.\tTo select the specified columns and rows from a given data frame. \n",
"\n",
"Select 'name' and 'score' columns in rows 1, 3, 5, 6 from the following data frame.\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"df2.index =[0,1,2,3,4,5,6,7,8,9]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score\n",
"1 Dima 9.0\n",
"3 James NaN\n",
"5 Michael 20.0\n",
"6 Matthew 14.5"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[[1,3,5,6],['name','score']]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emily</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score\n",
"0 Anastasia 12.5\n",
"1 Dima 9.0\n",
"2 Katherine 16.5\n",
"3 James NaN\n",
"4 Emily 9.0\n",
"5 Michael 20.0\n",
"6 Matthew 14.5\n",
"7 Laura NaN\n",
"8 Kevin 8.0\n",
"9 Jonas 19.0"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[:,['name','score']]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>attempts</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Emily</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Jonas</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Katherine</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Kevin</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Matthew</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Michael</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name attempts\n",
"0 Anastasia 1\n",
"1 Dima 3\n",
"2 Emily 2\n",
"3 James 3\n",
"4 Jonas 1\n",
"5 Katherine 2\n",
"6 Kevin 2\n",
"7 Laura 1\n",
"8 Matthew 1\n",
"9 Michael 3"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.groupby('name').aggregate({'attempts':max}).reset_index()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"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>attempts</th>\n",
" <th>name</th>\n",
" <th>score</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" attempts name score qualify\n",
"2 3 Michael 20.0 yes"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.groupby('attempts').max().reset_index().sort_values(by='attempts',ascending=False).head(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5.\tTo select the rows where the number of attempts in the examination is greater than 2. "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b Dima\n",
"d James\n",
"f Michael\n",
"Name: name, dtype: object"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2[df2['attempts']>2]['name']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>f</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"b Dima 9.0 3 no\n",
"d James NaN 3 no\n",
"f Michael 20.0 3 yes"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[df2['attempts']>2]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"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>attempts</th>\n",
" <th>name</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>3.0</td>\n",
" <td>Dima</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>3.0</td>\n",
" <td>James</td>\n",
" </tr>\n",
" <tr>\n",
" <th>f</th>\n",
" <td>3.0</td>\n",
" <td>Michael</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" attempts name\n",
"b 3.0 Dima\n",
"d 3.0 James\n",
"f 3.0 Michael"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[df2['attempts']>2,['attempts','name']]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6.\tTo count the number of rows and columns of a DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.shape[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7.\tTo select the rows where the score is missing, i.e. is NaN. "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3.0</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>h</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1.0</td>\n",
" <td>no</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"d James NaN 3.0 no\n",
"h Laura NaN 1.0 no"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[df2['score'].isna()==True]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9.0 2\n",
"19.0 1\n",
"8.0 1\n",
"14.5 1\n",
"20.0 1\n",
"16.5 1\n",
"12.5 1\n",
"Name: score, dtype: int64"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2['score'].value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2['score'].isna().sum()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>no</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"3 James NaN 3 no\n",
"7 Laura NaN 1 no"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2[df2['score'].isna()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"8.\tTo select the rows the score is between 15 and 20 (inclusive). "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"2 Katherine 16.5 2 yes\n",
"5 Michael 20.0 3 yes\n",
"9 Jonas 19.0 1 yes"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2[df2['score'].between(15,20)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"9.\tTo select the rows where number of attempts in the examination is less than 2 and score greater than 15. "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"9 Jonas 19.0 1 yes"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[(df2['attempts']<2) & (df2['score']>15)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"10.\tTo change the score in row 'd' to 11.5. "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emily</td>\n",
" <td>11.5</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"0 Anastasia 12.5 1 yes\n",
"1 Dima 9.0 3 no\n",
"2 Katherine 16.5 2 yes\n",
"3 James NaN 3 no\n",
"4 Emily 11.5 2 no\n",
"5 Michael 20.0 3 yes\n",
"6 Matthew 14.5 1 yes\n",
"7 Laura NaN 1 no\n",
"8 Kevin 8.0 2 no\n",
"9 Jonas 19.0 1 yes"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc[4,'score']=11.5\n",
"df2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"11.\t To calculate the sum of the examination attempts by the students. "
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"19"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2['attempts'].aggregate(sum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"12.\tTo calculate the mean score for each different student in DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"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>score</th>\n",
" </tr>\n",
" <tr>\n",
" <th>name</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Anastasia</th>\n",
" <td>12.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Dima</th>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Emily</th>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>James</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Jonas</th>\n",
" <td>19.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Katherine</th>\n",
" <td>16.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Kevin</th>\n",
" <td>8.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Laura</th>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Matthew</th>\n",
" <td>14.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Michael</th>\n",
" <td>20.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" score\n",
"name \n",
"Anastasia 12.5\n",
"Dima 9.0\n",
"Emily 9.0\n",
"James NaN\n",
"Jonas 19.0\n",
"Katherine 16.5\n",
"Kevin 8.0\n",
"Laura NaN\n",
"Matthew 14.5\n",
"Michael 20.0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.groupby('name').aggregate({'score':np.mean})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"13.\tTo append a new row 'k' to data frame with given values for each column. Now delete the new row and return the original DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" name score attempts qualify\n",
"a Anastasia 12.5 1 yes\n",
"b Dima 9.0 3 no\n",
"c Katherine 16.5 2 yes\n",
"d James NaN 3 no\n",
"e Emily 9.0 2 no\n",
"f Michael 20.0 3 yes\n",
"g Matthew 14.5 1 yes\n",
"h Laura NaN 1 no\n",
"i Kevin 8.0 2 no\n",
"j Jonas 19.0 1 yes\n",
"k smesh 15.0 1 yes\n"
]
}
],
"source": [
"df2.loc['k'] = ['smesh',15,1,'yes']\n",
"print(df2)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>e</th>\n",
" <td>Emily</td>\n",
" <td>9.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>f</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>g</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>h</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>i</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>j</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"a Anastasia 12.5 1 yes\n",
"b Dima 9.0 3 no\n",
"c Katherine 16.5 2 yes\n",
"d James NaN 3 no\n",
"e Emily 9.0 2 no\n",
"f Michael 20.0 3 yes\n",
"g Matthew 14.5 1 yes\n",
"h Laura NaN 1 no\n",
"i Kevin 8.0 2 no\n",
"j Jonas 19.0 1 yes"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.loc['k']\n",
"df2.drop('k')"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" <th>extra</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" <td>3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emily</td>\n",
" <td>11.5</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>no</td>\n",
" <td>7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" <td>8</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" <td>9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>k</th>\n",
" <td>smesh</td>\n",
" <td>15.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" <td>10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify extra\n",
"0 Anastasia 12.5 1 yes 0\n",
"1 Dima 9.0 3 no 1\n",
"2 Katherine 16.5 2 yes 2\n",
"3 James NaN 3 no 3\n",
"4 Emily 11.5 2 no 4\n",
"5 Michael 20.0 3 yes 5\n",
"6 Matthew 14.5 1 yes 6\n",
"7 Laura NaN 1 no 7\n",
"8 Kevin 8.0 2 no 8\n",
"9 Jonas 19.0 1 yes 9\n",
"k smesh 15.0 1 yes 10"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2['extra']=np.arange(11)\n",
"df2"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"df2.drop('extra',axis=1,inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"14.\tTo Sort the data frame first by 'name' in descending order, then by 'score' in ascending order:"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emily</td>\n",
" <td>11.5</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>no</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" <tr>\n",
" <th>k</th>\n",
" <td>smesh</td>\n",
" <td>15.0</td>\n",
" <td>1</td>\n",
" <td>yes</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"0 Anastasia 12.5 1 yes\n",
"1 Dima 9.0 3 no\n",
"4 Emily 11.5 2 no\n",
"3 James NaN 3 no\n",
"9 Jonas 19.0 1 yes\n",
"2 Katherine 16.5 2 yes\n",
"8 Kevin 8.0 2 no\n",
"7 Laura NaN 1 no\n",
"6 Matthew 14.5 1 yes\n",
"5 Michael 20.0 3 yes\n",
"k smesh 15.0 1 yes"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.sort_values(by=['name','score'],ascending=[True,False])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"15.\tTo replace the 'qualify' column contains the values 'yes' and 'no' with True and False. "
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>James</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emily</td>\n",
" <td>11.5</td>\n",
" <td>2</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>k</th>\n",
" <td>smesh</td>\n",
" <td>15.0</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"0 Anastasia 12.5 1 True\n",
"1 Dima 9.0 3 False\n",
"2 Katherine 16.5 2 True\n",
"3 James NaN 3 False\n",
"4 Emily 11.5 2 False\n",
"5 Michael 20.0 3 True\n",
"6 Matthew 14.5 1 True\n",
"7 Laura NaN 1 False\n",
"8 Kevin 8.0 2 False\n",
"9 Jonas 19.0 1 True\n",
"k smesh 15.0 1 True"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3=df2.replace({'yes':True,'no':False})\n",
"df3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"16.\tTo change the name 'James' to 'Suresh' in name column of the DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"df3.loc[df3['name']=='James','name']='Suresh'"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<ipython-input-31-365e2a0b86da>:1: SettingWithCopyWarning: \n",
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
"Try using .loc[row_indexer,col_indexer] = value instead\n",
"\n",
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
" df2[df2['name']=='James']['name'] ='Suresh'\n"
]
}
],
"source": [
"df2[df2['name']=='James']['name'] ='Suresh'"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>attempts</th>\n",
" <th>qualify</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Dima</td>\n",
" <td>9.0</td>\n",
" <td>3</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Katherine</td>\n",
" <td>16.5</td>\n",
" <td>2</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Suresh</td>\n",
" <td>NaN</td>\n",
" <td>3</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Emily</td>\n",
" <td>11.5</td>\n",
" <td>2</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Michael</td>\n",
" <td>20.0</td>\n",
" <td>3</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Matthew</td>\n",
" <td>14.5</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Laura</td>\n",
" <td>NaN</td>\n",
" <td>1</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>Kevin</td>\n",
" <td>8.0</td>\n",
" <td>2</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>Jonas</td>\n",
" <td>19.0</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>k</th>\n",
" <td>smesh</td>\n",
" <td>15.0</td>\n",
" <td>1</td>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score attempts qualify\n",
"0 Anastasia 12.5 1 True\n",
"1 Dima 9.0 3 False\n",
"2 Katherine 16.5 2 True\n",
"3 Suresh NaN 3 False\n",
"4 Emily 11.5 2 False\n",
"5 Michael 20.0 3 True\n",
"6 Matthew 14.5 1 True\n",
"7 Laura NaN 1 False\n",
"8 Kevin 8.0 2 False\n",
"9 Jonas 19.0 1 True\n",
"k smesh 15.0 1 True"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"17.\tTo delete the 'attempts' column from the DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"df3=df3.drop('attempts',axis=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"18.\tTo insert a new column in existing DataFrame. "
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"df3['color']=np.nan"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Bonus"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"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>name</th>\n",
" <th>score</th>\n",
" <th>qualify</th>\n",
" <th>color</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Anastasia</td>\n",
" <td>12.5</td>\n",
" <td>True</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" name score qualify color\n",
"0 Anastasia 12.5 True NaN"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3.loc[[True,False,False,False,False,False,False,False,False,False,False]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Iterating through DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key name\n",
"value 0 Mukul\n",
"1 Rohan\n",
"2 Shivam\n",
"3 Ragav\n",
"4 Monu\n",
"Name: name, dtype: object\n",
"key class\n",
"value 0 12\n",
"1 12\n",
"2 11\n",
"3 11\n",
"4 10\n",
"Name: class, dtype: int64\n",
"key total marks\n",
"value 0 454.0\n",
"1 433.0\n",
"2 NaN\n",
"3 NaN\n",
"4 456.0\n",
"Name: total marks, dtype: float64\n"
]
}
],
"source": [
"for key,value in df.iteritems():\n",
" print('key',key)\n",
" print('value',value)\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"key 0\n",
"value name Mukul\n",
"class 12\n",
"total marks 454\n",
"Name: 0, dtype: object\n",
"key 1\n",
"value name Rohan\n",
"class 12\n",
"total marks 433\n",
"Name: 1, dtype: object\n",
"key 2\n",
"value name Shivam\n",
"class 11\n",
"total marks NaN\n",
"Name: 2, dtype: object\n",
"key 3\n",
"value name Ragav\n",
"class 11\n",
"total marks NaN\n",
"Name: 3, dtype: object\n",
"key 4\n",
"value name Monu\n",
"class 10\n",
"total marks 456\n",
"Name: 4, dtype: object\n"
]
}
],
"source": [
"for key,value in df.iterrows():\n",
" print('key',key)\n",
" print('value',value)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"value Pandas(Index=0, name='Mukul', _2=12, _3=454.0)\n",
"value Pandas(Index=1, name='Rohan', _2=12, _3=433.0)\n",
"value Pandas(Index=2, name='Shivam', _2=11, _3=nan)\n",
"value Pandas(Index=3, name='Ragav', _2=11, _3=nan)\n",
"value Pandas(Index=4, name='Monu', _2=10, _3=456.0)\n"
]
}
],
"source": [
"for value in df.itertuples():\n",
" print('value',value)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAWYAAAD8CAYAAABErA6HAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3dd5wV1d3H8c93F4ioVCkimIhKjN3kUewGe0MRVCzYY9DEFksssRNNNJYY+8NjsMReIyp2RRMLxUSlWMBOEVTEBaQt/J4/7mW9LFtm2bu7w/B985rX3pk5d86Zu5cfh9+cOaOIwMzM0qOkqRtgZmZLc2A2M0sZB2Yzs5RxYDYzSxkHZjOzlHFgNjNLGQdmM7NqSBoiabqksdXsl6TrJU2U9K6kXxSjXgdmM7Pq3QHsVcP+vYEe+WUgcEsxKnVgNjOrRkS8CsyooUgf4K7IeRNoK6lLfettVt8D1EryrYVmlkyE6nuIljsPShxz5g2/+ARyPd0lBkfE4DpU1xX4omB9Un7b1DocYxkNH5jNzFIqH4TrEogrq+ofknp3Rh2YzSxbVO9Od11MAtYuWO8GTKnvQZ1jNrNsKS1NvtTfUOCo/OiMbYDvIqJeaQxwj9nMsqaIPWZJ9wG9gA6SJgEXA80BIuJWYBiwDzAR+B44thj1OjCbWbaoeImAiDislv0BnFS0CvOaLDC37HVpU1VtjWzu8IsTl51XPrMBW2Jpskqztg1z4JJGzTE3CPeYzSxbGvfiX4NwYDazbCliKqOpODCbWbaUOjCbmaWLe8xmZinjHLOZWcq4x2xmljIeLmdmljIlRbnVukk5MJtZtjjHbGaWMk5lmJmljC/+mZmljFMZZmYp48BsZpYyxZkAv0k5MJtZtrjHbGaWMr74Z2aWMh4uZ2aWMhlIZaz4fX4zs0IlpcmXWkjaS9IHkiZKOreK/W0kPSHp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K8LOcCbAx8AHTIr7cHLgHOyq+vUVD2MuCU/OsxQNf867b5nzcAA/KvWwAtm/r8VtaFXE94bMH6gcDzQCnQGfgc6AL0Ap4sKLcqsEr+dQ9gdFXHKyjfC5iZP9aPgMnApfl9pwHX5V+344cRVMcD1+RfXwK8teS7AhwD3Aj0Bf4FtKvu++Yl3Yv/+1M/uwAPR8TXABExQ0s/1mYTSZeR+6/x6sCz+e2vAXdIehB4NL/tDeB8Sd2ARyNiQmOcgCWyA3BfRCwCpkl6hdxT48sqlWsO3ChpC2AR8NMExx4VEVMBJH0EPJffPipizwQAAAGlSURBVAbYOf+6G/CApC7k/tH+pOD9QyNibsH6zsCWwB4RsaR9VX3fLMWcyqgfATUNBL8DODkiNgUuBVYBiIgTyT3kdm3gbUlrRMS9wP7AXOBZSbs0ZMOtTpLOQHY6MA3YnFxwbJHgPfMLXi8uWF/MD9Py3gDcmP8enUD+e5Q3p9LxPiY3NW/FPwpVfd+SnIw1HQfm+nkR6L/kiy6pfaX9rYCpkpoDA5ZslLReRIyIiIuAr4G1Ja0LfBwR1wNDyT1n0ZrGLHK/uyVeBQ6RVJp/SvxOwMgqyrUBpkbEYuBIcqmPYmhDLs0BcHQtZT8D+gF3SdoYqv6+Fald1kCcyqiHiBgn6XLgFUmLgP8CnxYUuRAYQe4vyxh++Et8Vf7insgF93fIzXl9hKSFwJfAoEY5CVtGRHwj6bX8ELengbOBbcn9ngI4OyK+lPQNUC7pHXL/O7oZeETSwcDLLNubXV6XAA9Jmgy8CXSvpf0fSBqQf89+VP19sxTzLdlmZinjVIaZWco4MJuZpYwDs5lZyjgwm5mljAOzmVnKODCbmaWMA7OZWcr8P24VkFtSk/nzAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 2 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import seaborn as sns\n",
"import matplotlib.pyplot as plt\n",
"sns.heatmap(df.corr(),cmap='GnBu',annot=np.array([['',''],list(df.corr()['total marks'])]),fmt = 's',linewidths=10,linecolor='r')\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"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>class</th>\n",
" <th>total marks</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>class</th>\n",
" <td>1.000000</td>\n",
" <td>-0.566429</td>\n",
" </tr>\n",
" <tr>\n",
" <th>total marks</th>\n",
" <td>-0.566429</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" class total marks\n",
"class 1.000000 -0.566429\n",
"total marks -0.566429 1.000000"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.corr()"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"class -0.566429\n",
"total marks 1.000000\n",
"Name: total marks, dtype: float64"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.corr()['total marks']"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"total marks -0.566429\n",
"Name: class, dtype: float64"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.corr().loc['class',['total marks']]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment