diff --git a/.ipynb_checkpoints/EDApt1Exercises-checkpoint.ipynb b/.ipynb_checkpoints/EDApt1Exercises-checkpoint.ipynb index 0b0ec10..335d362 100644 --- a/.ipynb_checkpoints/EDApt1Exercises-checkpoint.ipynb +++ b/.ipynb_checkpoints/EDApt1Exercises-checkpoint.ipynb @@ -12,12 +12,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "c18c6c3a-909e-4be2-bec6-fffe39a15f18", "metadata": {}, "outputs": [], "source": [ - "# import the pandas library and use the alias 'pd'\n" + "# import the pandas library and use the alias 'pd'\n", + "import pandas as pd\n" ] }, { @@ -33,39 +34,119 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "id": "e31b74b5-2f8e-4126-bf65-be577a3e5b06", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The fruits list are \n", + " Oranges 10\n", + "Apples 20\n", + "Banana 30\n", + "Pears 40\n", + "Mango 50\n", + "dtype: int64\n", + "the prices Apples 20\n", + "Banana 30\n", + "dtype: int64\n", + "the prices 50\n", + "Favourite fruits are \n", + " Anna apple\n", + "John banana\n", + "Kim cherry\n", + "dtype: object\n", + "Model details\n", + " brand Ford\n", + "model Mustang\n", + "year 1964\n", + "dtype: object\n", + "model details Ford\n", + "model details\n", + " brand Ford\n", + "model Mustang\n", + "year 1964\n", + "dtype: object\n" + ] + } + ], "source": [ "#create the s1 series from the list: [10,20,30,40,50]\n", "\n", + "s1=pd.Series([10,20,30,40,50], index=['Oranges','Apples','Banana','Pears','Mango'])\n", + "print(\"The fruits list are \\n \", s1)\n", + "print(\"the prices\",s1[1:3])\n", + "print(\"the prices\",s1[\"Mango\"])\n", + "\n", "# additional practice\n", "# create a series called s_tuple from the tuple: (\"apple\", \"banana\", \"cherry\")\n", + "s_tuple=pd.Series((\"apple\", \"banana\", \"cherry\"),index=(\"Anna\",\"John\",\"Kim\"))\n", + "print(\"Favourite fruits are \\n\",s_tuple)\n", "\n", - "# create a series called s_dictionary from the dictionary: {\"brand\": \"Ford\", \"model\": \"Mustang\", \"year\": 1964}\n" + "# create a series called s_dictionary from the dictionary: {\"brand\": \"Ford\", \"model\": \"Mustang\", \"year\": 1964}\n", + "dict_model={\"brand\": \"Ford\", \"model\": \"Mustang\", \"year\": 1964}\n", + "s_dictionary=pd.Series(dict_model)\n", + "print(\"Model details\\n\",s_dictionary)\n", + "print(\"model details\",s_dictionary[\"brand\"])\n", + "print(\"model details\\n\",s_dictionary[0:3])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "id": "76d730b9-fd65-4278-af53-22c1674005a9", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The s1 series values are\n", + ": Oranges 10\n", + "Apples 20\n", + "Banana 30\n", + "Pears 40\n", + "Mango 50\n", + "dtype: int64\n", + "The s1 index values are:\n", + " 10\n", + "\n", + "\n", + "The s_tuple series values are: Anna apple\n", + "John banana\n", + "Kim cherry\n", + "dtype: object\n", + "The s_dictionary series values are\n", + ": brand Ford\n", + "model Mustang\n", + "year 1964\n", + "dtype: object\n", + "The s_dictionary index values are:\n", + " Ford\n" + ] + } + ], "source": [ "# fill in the blank with the series values and index values\n", - "print(\"The s1 series values are:\", #s1 values#)\n", - "print(\"The s1 index values are:\", #s1 index values#)\n", - "print(\"\\n\")\n", - "print(\"The s_tuple series values are:\", #s_tuple values#)\n", - "print(\"The s_tuple index values are:\", #s_tuple index values#)\n", + "# print(\"The s1 series values are:\", #s1 values#)\n", + "print(\"The s1 series values are\\n:\",s1[0:5])\n", + "# print(\"The s1 index values are:\", #s1 index values#)\n", + "print(\"The s1 index values are:\\n\", s1['Oranges'])\n", "print(\"\\n\")\n", - "print(\"The s_dictionary series values are:\", #s_dictionary values#)\n", - "print(\"The s_dictionary index values are:\", #s_dictionary index values#)" + "# print(\"The s_tuple series values are:\", #s_tuple values#)\n", + "print(\"The s_tuple series values are:\", s_tuple[0:3])\n", + "# print(\"The s_tuple index values are:\", #s_tuple index values#)\n", + "# print(\"The s_tuple index values are:\",s_tuple(\"John\"))\n", + "# print(\"\\n\")\n", + "# print(\"The s_dictionary series values are:\", #s_dictionary values#)\n", + "print(\"The s_dictionary series values are\\n:\", s_dictionary[0:3])\n", + "print(\"The s_dictionary index values are:\\n\", s_dictionary[\"brand\"])" ] }, { @@ -80,22 +161,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "id": "f7794bcc-c9f0-4c21-80f5-c71258532c13", "metadata": {}, "outputs": [], "source": [ - "# create the s2 series from the list [80,93,78,85,97] and with labeled indexes ['English','Science','Social','Tamil','Maths']\n" + "# create the s2 series from the list [80,93,78,85,97] and with labeled indexes ['English','Science','Social','Tamil','Maths']\n", + "s2=pd.Series([80,93,78,85,97],index=['English','Science','Social','Tamil','Maths'])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "id": "5674059a-a773-4bc0-b6f1-94ff3d5e2bf6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The marks obtained by students are:\n", + " English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "Maths 97\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# print the s2 series" + "# print the s2 series\n", + "print(\"The marks obtained by students are:\\n\",s2)" ] }, { @@ -108,24 +205,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "id": "36a58fa6-c808-4ca4-8209-c53b97d298bf", "metadata": {}, "outputs": [], "source": [ "# assign the s2 series name to be \"Student Marks\"\n", + "student_marks=[80,93,78,85,97]\n", "\n", - "# assign the s2 index name to be \"Subject\"\n" + "# assign the s2 index name to be \"Subject\"\n", + "subject=['English','Science','Social','Tamil','Maths']" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "id": "0ead4e61-9a2d-4efc-bdb6-aaaad033654f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The marks obtained by students are:\n", + " English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "Maths 97\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# print the s2 series again to see the newly assigned name and index name\n" + "# print the s2 series again to see the newly assigned name and index name\n", + "s2=pd.Series((student_marks),index=(subject))\n", + "print(\"The marks obtained by students are:\\n\",s2)" ] }, { @@ -143,34 +258,69 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "fa73dcf8-8afb-4a9a-a11f-2549a078137d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "first element in the s2 series:\n", + " English 80\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# return the first element in the s2 series\n" + "# return the first element in the s2 series\n", + "\n", + "print(\"first element in the s2 series:\\n\",s2[0:1])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "id": "325c7f72-35e7-40cf-ab84-756ad241ba53", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "first element in the s2 series:\n", + " English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "dtype: int64\n" + ] + } + ], "source": [ "# slicing using default integer index\n", - "# return the elements starting at position one and ending with the element at index 3\n" + "# return the elements starting at position one and ending with the element at index 3\n", + "print(\"first tp third elements in the s2 series:\\n\",s2[0:4])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "id": "47f1247d-bbbb-482b-b420-1e26c4acd7a8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the element associated with the index 'Tamil' 85\n" + ] + } + ], "source": [ "# Slicing using string index\n", - "# return the element associated with the index 'Tamil'\n" + "# return the element associated with the index 'Tamil'\n", + "print(\"the element associated with the index 'Tamil'\",s2[\"Tamil\"])\n" ] }, { @@ -191,43 +341,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "id": "e518a820-ab1f-41c1-8740-7df589d2e145", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Orange': 80, 'Apples': 210, 'Bananas': 50, 'Grapes': 90, 'Watermelon': 70}\n" + ] + } + ], "source": [ - "# create a dictionary of fruit names and their prices. Use the values from the article\n" + "# create a dictionary of fruit names and their prices. Use the values from the article\n", + "dict_fruits = { 'Orange':80,\n", + " 'Apples':210,\n", + " 'Bananas':50,\n", + " 'Grapes':90,\n", + " 'Watermelon':70}\n", + "print(dict_fruits)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "id": "f55a91ad-4954-44a7-ab9e-0dc9f049866f", "metadata": {}, "outputs": [], "source": [ "# Lets convert this dictionary into a series\n", - "# use the pandas method .Series() \n" + "# use the pandas method .Series() \n", + "fruits=pd.Series(dict_fruits)\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "id": "e7ffad90-5e49-46bb-93ba-d6b66682f8b0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orange 80\n", + "Apples 210\n", + "Bananas 50\n", + "Grapes 90\n", + "Watermelon 70\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# print the series\n" + "# print the series\n", + "print(fruits)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "id": "a4477e73-b2f8-472c-a8ce-d25455fceda0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Price of grapes:\n", + " 90\n" + ] + } + ], "source": [ - "# Slice the series and retrieve price of grapes\n" + "# Slice the series and retrieve price of grapes\n", + "print(\"Price of grapes:\\n\",fruits[3])\n" ] }, { @@ -244,22 +434,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "id": "7b8c6007-fb21-4609-b98f-5e8a7359ba42", "metadata": {}, "outputs": [], "source": [ - "# create a dataframe from the s2 series\n" + "# create a dataframe from the s2 series\n", + "df_marks = pd.DataFrame(s2, columns=['Student1'])\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "id": "3440fb09-d503-4471-8d68-5fb91c77b0c8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The dataframe created from series is\n", + " Student1\n", + "English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "Maths 97\n" + ] + } + ], "source": [ - "# print the dataframe\n" + "# print the dataframe\n", + "print(\"The dataframe created from series is\\n\",df_marks)" ] }, { @@ -278,33 +485,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "id": "4ce55704-083e-480d-9acd-44304b341f31", "metadata": {}, "outputs": [], "source": [ - "# Create height series using [5.3, 6.2,5.8,5.0,5.5] for values, ['Person 1','Person 2','Person 3','Person 4','Person 5'] for indexes\n", + "# Create height series1 using [5.3, 6.2,5.8,5.0,5.5] for values, ['Person 1','Person 2','Person 3','Person 4','Person 5'] for indexes\n", + "height = pd.Series([5.3, 6.2,5.8,5.0,5.5], index=['Person 1','Person 2','Person 3','Person 4','Person 5'])\n", "\n", - "\n", - "# Create weight series (in kgs) using [65,89,75,60,59] for values and the same indexes as height\n" + "# Create weight series2 (in kgs) using [65,89,75,60,59] for values and the same indexes as height\n", + "weight = pd.Series([65,89,75,60,59], index=['Person 1','Person 2','Person 3','Person 4','Person 5'])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "id": "1bd7b205-0443-454e-85d5-bcd9e5478aea", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Person table details are:\n", + " height weight\n", + "Person 1 5.3 65\n", + "Person 2 6.2 89\n", + "Person 3 5.8 75\n", + "Person 4 5.0 60\n", + "Person 5 5.5 59\n" + ] + } + ], "source": [ "# Create a dataframe from the height and weight series above\n", + "df_person = pd.DataFrame({'height': height, 'weight': weight})\n", "\n", - "# print the dataframe\n" + "# print the dataframe\n", + "print(\"The Person table details are:\\n\", df_person)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -318,7 +542,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.10.9" } }, "nbformat": 4, diff --git a/EDApt1Exercises.ipynb b/EDApt1Exercises.ipynb index 0b0ec10..335d362 100644 --- a/EDApt1Exercises.ipynb +++ b/EDApt1Exercises.ipynb @@ -12,12 +12,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "c18c6c3a-909e-4be2-bec6-fffe39a15f18", "metadata": {}, "outputs": [], "source": [ - "# import the pandas library and use the alias 'pd'\n" + "# import the pandas library and use the alias 'pd'\n", + "import pandas as pd\n" ] }, { @@ -33,39 +34,119 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "id": "e31b74b5-2f8e-4126-bf65-be577a3e5b06", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The fruits list are \n", + " Oranges 10\n", + "Apples 20\n", + "Banana 30\n", + "Pears 40\n", + "Mango 50\n", + "dtype: int64\n", + "the prices Apples 20\n", + "Banana 30\n", + "dtype: int64\n", + "the prices 50\n", + "Favourite fruits are \n", + " Anna apple\n", + "John banana\n", + "Kim cherry\n", + "dtype: object\n", + "Model details\n", + " brand Ford\n", + "model Mustang\n", + "year 1964\n", + "dtype: object\n", + "model details Ford\n", + "model details\n", + " brand Ford\n", + "model Mustang\n", + "year 1964\n", + "dtype: object\n" + ] + } + ], "source": [ "#create the s1 series from the list: [10,20,30,40,50]\n", "\n", + "s1=pd.Series([10,20,30,40,50], index=['Oranges','Apples','Banana','Pears','Mango'])\n", + "print(\"The fruits list are \\n \", s1)\n", + "print(\"the prices\",s1[1:3])\n", + "print(\"the prices\",s1[\"Mango\"])\n", + "\n", "# additional practice\n", "# create a series called s_tuple from the tuple: (\"apple\", \"banana\", \"cherry\")\n", + "s_tuple=pd.Series((\"apple\", \"banana\", \"cherry\"),index=(\"Anna\",\"John\",\"Kim\"))\n", + "print(\"Favourite fruits are \\n\",s_tuple)\n", "\n", - "# create a series called s_dictionary from the dictionary: {\"brand\": \"Ford\", \"model\": \"Mustang\", \"year\": 1964}\n" + "# create a series called s_dictionary from the dictionary: {\"brand\": \"Ford\", \"model\": \"Mustang\", \"year\": 1964}\n", + "dict_model={\"brand\": \"Ford\", \"model\": \"Mustang\", \"year\": 1964}\n", + "s_dictionary=pd.Series(dict_model)\n", + "print(\"Model details\\n\",s_dictionary)\n", + "print(\"model details\",s_dictionary[\"brand\"])\n", + "print(\"model details\\n\",s_dictionary[0:3])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "id": "76d730b9-fd65-4278-af53-22c1674005a9", "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The s1 series values are\n", + ": Oranges 10\n", + "Apples 20\n", + "Banana 30\n", + "Pears 40\n", + "Mango 50\n", + "dtype: int64\n", + "The s1 index values are:\n", + " 10\n", + "\n", + "\n", + "The s_tuple series values are: Anna apple\n", + "John banana\n", + "Kim cherry\n", + "dtype: object\n", + "The s_dictionary series values are\n", + ": brand Ford\n", + "model Mustang\n", + "year 1964\n", + "dtype: object\n", + "The s_dictionary index values are:\n", + " Ford\n" + ] + } + ], "source": [ "# fill in the blank with the series values and index values\n", - "print(\"The s1 series values are:\", #s1 values#)\n", - "print(\"The s1 index values are:\", #s1 index values#)\n", - "print(\"\\n\")\n", - "print(\"The s_tuple series values are:\", #s_tuple values#)\n", - "print(\"The s_tuple index values are:\", #s_tuple index values#)\n", + "# print(\"The s1 series values are:\", #s1 values#)\n", + "print(\"The s1 series values are\\n:\",s1[0:5])\n", + "# print(\"The s1 index values are:\", #s1 index values#)\n", + "print(\"The s1 index values are:\\n\", s1['Oranges'])\n", "print(\"\\n\")\n", - "print(\"The s_dictionary series values are:\", #s_dictionary values#)\n", - "print(\"The s_dictionary index values are:\", #s_dictionary index values#)" + "# print(\"The s_tuple series values are:\", #s_tuple values#)\n", + "print(\"The s_tuple series values are:\", s_tuple[0:3])\n", + "# print(\"The s_tuple index values are:\", #s_tuple index values#)\n", + "# print(\"The s_tuple index values are:\",s_tuple(\"John\"))\n", + "# print(\"\\n\")\n", + "# print(\"The s_dictionary series values are:\", #s_dictionary values#)\n", + "print(\"The s_dictionary series values are\\n:\", s_dictionary[0:3])\n", + "print(\"The s_dictionary index values are:\\n\", s_dictionary[\"brand\"])" ] }, { @@ -80,22 +161,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "id": "f7794bcc-c9f0-4c21-80f5-c71258532c13", "metadata": {}, "outputs": [], "source": [ - "# create the s2 series from the list [80,93,78,85,97] and with labeled indexes ['English','Science','Social','Tamil','Maths']\n" + "# create the s2 series from the list [80,93,78,85,97] and with labeled indexes ['English','Science','Social','Tamil','Maths']\n", + "s2=pd.Series([80,93,78,85,97],index=['English','Science','Social','Tamil','Maths'])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 67, "id": "5674059a-a773-4bc0-b6f1-94ff3d5e2bf6", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The marks obtained by students are:\n", + " English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "Maths 97\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# print the s2 series" + "# print the s2 series\n", + "print(\"The marks obtained by students are:\\n\",s2)" ] }, { @@ -108,24 +205,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "id": "36a58fa6-c808-4ca4-8209-c53b97d298bf", "metadata": {}, "outputs": [], "source": [ "# assign the s2 series name to be \"Student Marks\"\n", + "student_marks=[80,93,78,85,97]\n", "\n", - "# assign the s2 index name to be \"Subject\"\n" + "# assign the s2 index name to be \"Subject\"\n", + "subject=['English','Science','Social','Tamil','Maths']" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "id": "0ead4e61-9a2d-4efc-bdb6-aaaad033654f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The marks obtained by students are:\n", + " English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "Maths 97\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# print the s2 series again to see the newly assigned name and index name\n" + "# print the s2 series again to see the newly assigned name and index name\n", + "s2=pd.Series((student_marks),index=(subject))\n", + "print(\"The marks obtained by students are:\\n\",s2)" ] }, { @@ -143,34 +258,69 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "fa73dcf8-8afb-4a9a-a11f-2549a078137d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "first element in the s2 series:\n", + " English 80\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# return the first element in the s2 series\n" + "# return the first element in the s2 series\n", + "\n", + "print(\"first element in the s2 series:\\n\",s2[0:1])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "id": "325c7f72-35e7-40cf-ab84-756ad241ba53", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "first element in the s2 series:\n", + " English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "dtype: int64\n" + ] + } + ], "source": [ "# slicing using default integer index\n", - "# return the elements starting at position one and ending with the element at index 3\n" + "# return the elements starting at position one and ending with the element at index 3\n", + "print(\"first tp third elements in the s2 series:\\n\",s2[0:4])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "id": "47f1247d-bbbb-482b-b420-1e26c4acd7a8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the element associated with the index 'Tamil' 85\n" + ] + } + ], "source": [ "# Slicing using string index\n", - "# return the element associated with the index 'Tamil'\n" + "# return the element associated with the index 'Tamil'\n", + "print(\"the element associated with the index 'Tamil'\",s2[\"Tamil\"])\n" ] }, { @@ -191,43 +341,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "id": "e518a820-ab1f-41c1-8740-7df589d2e145", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Orange': 80, 'Apples': 210, 'Bananas': 50, 'Grapes': 90, 'Watermelon': 70}\n" + ] + } + ], "source": [ - "# create a dictionary of fruit names and their prices. Use the values from the article\n" + "# create a dictionary of fruit names and their prices. Use the values from the article\n", + "dict_fruits = { 'Orange':80,\n", + " 'Apples':210,\n", + " 'Bananas':50,\n", + " 'Grapes':90,\n", + " 'Watermelon':70}\n", + "print(dict_fruits)\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "id": "f55a91ad-4954-44a7-ab9e-0dc9f049866f", "metadata": {}, "outputs": [], "source": [ "# Lets convert this dictionary into a series\n", - "# use the pandas method .Series() \n" + "# use the pandas method .Series() \n", + "fruits=pd.Series(dict_fruits)\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "id": "e7ffad90-5e49-46bb-93ba-d6b66682f8b0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Orange 80\n", + "Apples 210\n", + "Bananas 50\n", + "Grapes 90\n", + "Watermelon 70\n", + "dtype: int64\n" + ] + } + ], "source": [ - "# print the series\n" + "# print the series\n", + "print(fruits)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "id": "a4477e73-b2f8-472c-a8ce-d25455fceda0", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Price of grapes:\n", + " 90\n" + ] + } + ], "source": [ - "# Slice the series and retrieve price of grapes\n" + "# Slice the series and retrieve price of grapes\n", + "print(\"Price of grapes:\\n\",fruits[3])\n" ] }, { @@ -244,22 +434,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "id": "7b8c6007-fb21-4609-b98f-5e8a7359ba42", "metadata": {}, "outputs": [], "source": [ - "# create a dataframe from the s2 series\n" + "# create a dataframe from the s2 series\n", + "df_marks = pd.DataFrame(s2, columns=['Student1'])\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "id": "3440fb09-d503-4471-8d68-5fb91c77b0c8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The dataframe created from series is\n", + " Student1\n", + "English 80\n", + "Science 93\n", + "Social 78\n", + "Tamil 85\n", + "Maths 97\n" + ] + } + ], "source": [ - "# print the dataframe\n" + "# print the dataframe\n", + "print(\"The dataframe created from series is\\n\",df_marks)" ] }, { @@ -278,33 +485,50 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "id": "4ce55704-083e-480d-9acd-44304b341f31", "metadata": {}, "outputs": [], "source": [ - "# Create height series using [5.3, 6.2,5.8,5.0,5.5] for values, ['Person 1','Person 2','Person 3','Person 4','Person 5'] for indexes\n", + "# Create height series1 using [5.3, 6.2,5.8,5.0,5.5] for values, ['Person 1','Person 2','Person 3','Person 4','Person 5'] for indexes\n", + "height = pd.Series([5.3, 6.2,5.8,5.0,5.5], index=['Person 1','Person 2','Person 3','Person 4','Person 5'])\n", "\n", - "\n", - "# Create weight series (in kgs) using [65,89,75,60,59] for values and the same indexes as height\n" + "# Create weight series2 (in kgs) using [65,89,75,60,59] for values and the same indexes as height\n", + "weight = pd.Series([65,89,75,60,59], index=['Person 1','Person 2','Person 3','Person 4','Person 5'])\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "id": "1bd7b205-0443-454e-85d5-bcd9e5478aea", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The Person table details are:\n", + " height weight\n", + "Person 1 5.3 65\n", + "Person 2 6.2 89\n", + "Person 3 5.8 75\n", + "Person 4 5.0 60\n", + "Person 5 5.5 59\n" + ] + } + ], "source": [ "# Create a dataframe from the height and weight series above\n", + "df_person = pd.DataFrame({'height': height, 'weight': weight})\n", "\n", - "# print the dataframe\n" + "# print the dataframe\n", + "print(\"The Person table details are:\\n\", df_person)\n" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -318,7 +542,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.10.9" } }, "nbformat": 4,