diff --git a/AD450_math.py b/AD450_math.py new file mode 100644 index 0000000..357720a --- /dev/null +++ b/AD450_math.py @@ -0,0 +1,5 @@ +def add(num1, num2): + return num1 + num2 + +def multiply(num1, num2): + return num1 * num2 \ No newline at end of file diff --git a/week_2_intro_ipython.ipynb b/week_2_intro_ipython.ipynb index 1a469f7..7746dc9 100644 --- a/week_2_intro_ipython.ipynb +++ b/week_2_intro_ipython.ipynb @@ -24,12 +24,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "id": "BQybla17FRh5" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My name is Julien.\n", + "I am a student at North Seattle College\n" + ] + } + ], + "source": [ + "name = \"Julien\"\n", + "print(\"My name is \" + name + \".\\nI am a student at North Seattle College\")" + ] }, { "cell_type": "markdown", @@ -43,12 +55,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": { "id": "P7QDyWncFRh8" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "My phone number is 2069991111\n" + ] + } + ], + "source": [ + "phone = 2069991111\n", + "print(\"My phone number is \" + str(phone))" + ] }, { "cell_type": "markdown", @@ -62,12 +85,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": { "id": "OnOau948FRh9" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello my name is Julien and my phone number is 206-999-1111\n" + ] + } + ], + "source": [ + "name = \"Julien\"\n", + "phone_number = \"206-999-1111\"\n", + "\n", + "print(f\"Hello my name is {name} and my phone number is {phone_number}\")" + ] }, { "cell_type": "markdown", @@ -82,12 +118,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": { "id": "5x2rIFzRFRh9" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I graduate in June of 2027\n" + ] + } + ], + "source": [ + "month, year = \"June\", 2027\n", + "print(f\"I graduate in {month} of {year}\")" + ] }, { "cell_type": "markdown", @@ -109,12 +156,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "id": "OkyeAVPXFRh-" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "58.5\n" + ] + } + ], + "source": [ + "ans = 12 * 5 - 3 / 2\n", + "print(ans)\n", + "# it is float because there are trailing decimals\n" + ] }, { "cell_type": "markdown", @@ -136,12 +195,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": { "id": "Lo-LEy8bKYmG" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "31 days\n" + ] + } + ], + "source": [ + "month = \"October\"\n", + "\n", + "if month == \"October\":\n", + " print(\"31 days\")\n", + "else:\n", + " print(\"30 days\")\n", + " \n" + ] }, { "cell_type": "markdown", @@ -157,12 +232,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "id": "qscUkh813QKH" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "between 100 and 150\n" + ] + } + ], + "source": [ + "num = 140\n", + "\n", + "if num > 150:\n", + " print(\"greater than 150\")\n", + "elif num > 100:\n", + " print(\"between 100 and 150\")\n", + "else:\n", + " print(\"less than 100\")" + ] }, { "cell_type": "markdown", @@ -177,12 +269,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": { "id": "1vxg4nMbFRh_" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number is divisible by 5 and 10\n", + "Number is divisible by 7 or 2\n" + ] + } + ], + "source": [ + "num = 70 \n", + "if num % 5 == 0 and num % 10 == 0:\n", + " print(\"Number is divisible by 5 and 10\")\n", + "\n", + "if num % 7 == 0 or num % 2 == 0:\n", + " print(\"Number is divisible by 7 or 2\")" + ] }, { "cell_type": "markdown", @@ -195,12 +303,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "id": "JY_Ci5v-FRiA" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "7\n", + "14\n", + "21\n", + "28\n", + "35\n", + "42\n", + "49\n", + "56\n", + "63\n", + "70\n", + "77\n", + "84\n", + "91\n", + "98\n" + ] + } + ], + "source": [ + "for i in range(0, 100):\n", + " if i % 7 == 0:\n", + " print(i)" + ] }, { "cell_type": "markdown", @@ -213,12 +347,30 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "4a1D64hpFRiA" - }, - "outputs": [], - "source": [] + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 3 6 9 \n", + "18\n" + ] + } + ], + "source": [ + "i = 0\n", + "sums = 0\n", + "nums = \"\"\n", + "while i < 10: \n", + " if i % 3 == 0:\n", + " nums += str(i) + \" \"\n", + " sums += i\n", + " i += 1\n", + "print(nums)\n", + "print(sums)\n" + ] }, { "cell_type": "markdown", @@ -241,12 +393,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": { "id": "ntBnYxzi1RPc" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Student 2\n", + "Student 4\n" + ] + } + ], + "source": [ + "sets = [[1,], [2], [3], [4], [5]]\n", + "for i in sets:\n", + " for j in i:\n", + " if j % 2 == 0:\n", + " print(\"Student \" + str(j))\n" + ] }, { "cell_type": "markdown", @@ -259,12 +426,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": { "id": "lppms8ctzfRI" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hogwarts\n", + "s\n" + ] + } + ], + "source": [ + "school = \"Hogwarts\"\n", + "first = school[0:3]\n", + "second = school[3:8]\n", + "print(first + second)\n", + "print(school[-1])" + ] }, { "cell_type": "markdown", @@ -280,12 +462,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": { "id": "zw2NezoM0zfl" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Thor is a character in the Marvel Cinematic Universe\n", + "She-Hulk is a character in the Marvel Cinematic Universe\n", + "Loki is a character in the Marvel Cinematic Universe\n" + ] + } + ], + "source": [ + "chars = [\"Thor\", \"She-Hulk\", \"Loki\"]\n", + "for char in chars:\n", + " print(char +\" is a character in the Marvel Cinematic Universe\")" + ] }, { "cell_type": "markdown", @@ -299,12 +495,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": { "id": "rbyZif9a1b2V" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "janurary has 31 days\n", + "february has 28 days\n", + "march has 31 days\n", + "april has 30 days\n", + "may has 31 days\n", + "june has 30 days\n" + ] + } + ], + "source": [ + "months = [[\"janurary\", 31], [\"february\", 28], [\"march\", 31], [\"april\", 30], [\"may\", 31], [\"june\", 30]]\n", + "for i in range(len(months)):\n", + " print(f\"{months[i][0]} has {months[i][1]} days\")\n" + ] }, { "cell_type": "markdown", @@ -321,12 +534,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": { "id": "p0YE0nqs4Hcz" }, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Breed': 'Corgi', 'Age': 13, 'Type': 'Cattle herding'}\n", + "Corgi\n", + "The Corgi is expected to live about 13 years\n" + ] + } + ], + "source": [ + "dog = {\n", + " \"Breed\": \"Corgi\",\n", + " \"Age\": 13,\n", + " \"Type\": \"Cattle herding\"\n", + "}\n", + "print(dog)\n", + "print(dog[\"Breed\"])\n", + "print(f\"The {dog['Breed']} is expected to live about {dog['Age']} years\")" + ] }, { "cell_type": "markdown", @@ -337,9 +569,20 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import AD450_math\n", "\n", @@ -359,9 +602,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Cell with import error\n", "AD450_math.multiply(2, 2)" @@ -369,18 +623,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], "source": [ - "# Reload cell" + "# Reload cell\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Cell with correct output \n", "AD450_math.multiply(2, 2)" @@ -400,29 +665,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, "outputs": [], "source": [ - "# Create a" + "# Create a\n", + "a = 1 " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], "source": [ - "# Print a" + "# Print a\n", + "print(a)" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ - "# Set a to 4" + "# Set a to 4\n", + "a = 4 " ] } ], @@ -431,7 +707,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "my_env_2", + "display_name": "base", "language": "python", "name": "python3" }, @@ -445,7 +721,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.16" + "version": "3.13.5" } }, "nbformat": 4, diff --git a/week_3_numpy.ipynb b/week_3_numpy.ipynb index 1db8573..893f386 100644 --- a/week_3_numpy.ipynb +++ b/week_3_numpy.ipynb @@ -57,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": { "id": "Pt63xmy1qLll" }, @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 153, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -83,19 +83,37 @@ "id": "AVByQUYKxtNG", "outputId": "238e50f0-366d-41de-a44a-6cf3e063650c" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 2 3 4]\n", + " [5 6 7 8]]\n", + "\n", + "Inspect the array\n", + "--------------\n", + "dimensions: 2\n", + "shape:\t (2, 4)\n", + "size:\t 8\n", + "datatype: int64\n", + "bytes:\t 64\n" + ] + } + ], "source": [ "# create an array called numbers with 2 rows and 4 columns from literal values. Have your literal values be the integers 1-8\n", - "numbers = None #Replace None with array defintion\n", + "numbers = np.array([[1,2,3,4], [5,6,7,8]])\n", "print(numbers)\n", + "numbers_bytes = numbers.nbytes\n", "\n", "# Replace None below with each value for the array. \n", "print(\"\\nInspect the array\\n--------------\")\n", - "print(\"dimensions:\", None) # ndarrays have dimensions\n", - "print(\"shape:\\t\", None) # ndarrays have shape (numer of rows & columns)\n", - "print(\"size:\\t\", None) # number of elements in the array\n", - "print(\"datatype\", None) # numpy determines the datatype\n", - "print(\"bytes:\\t\", None) # total bytes consumed by the array" + "print(\"dimensions:\", numbers.ndim) # ndarrays have dimensions\n", + "print(\"shape:\\t\", numbers.shape) # ndarrays have shape (numer of rows & columns)\n", + "print(\"size:\\t\", numbers.size) # number of elements in the array\n", + "print(\"datatype:\", numbers.dtype) # numpy determines the datatype\n", + "print(\"bytes:\\t\", numbers_bytes) # total bytes consumed by the array" ] }, { @@ -109,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -117,34 +135,63 @@ "id": "pTXgMRYRQS3v", "outputId": "010d4702-bc29-4f87-980e-66fe9b43436a" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]\n", + " [1. 1. 1. 1.]] \n", + "\n", + "[[0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]\n", + " [0. 0. 0. 0.]] \n", + "\n", + "[[-0.00791867 0.60836559]\n", + " [ 0.32089487 0.94509106]] \n", + "\n", + "[[0.375 0.75 ]\n", + " [1.125 1.5 ]\n", + " [1.875 2.25 ]] \n", + "\n", + "[['x' 'x']\n", + " ['x' 'x']] \n", + "\n", + "[10 15 20] \n", + "\n", + "[0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] \n", + "\n" + ] + } + ], "source": [ "# Create an 3 by 4 array of ones\n", - "ones = None\n", + "ones = np.ones([3, 4])\n", "print(ones, \"\\n\")\n", "\n", "# Create an 3 by 4 array of zeros\n", - "zeros = None\n", + "zeros = np.zeros([3, 4])\n", "print(zeros, \"\\n\")\n", "\n", "# Create an 2 by 2 array with random values\n", - "rando = None\n", + "rando = np.random.standard_normal(size=(2, 2))\n", "print(rando, \"\\n\")\n", "\n", "# Create 3 by 2 an empty array\n", - "empty = None\n", + "empty = np.empty((3, 2))\n", "print(empty, \"\\n\")\n", "\n", "# Create a 2 by 2 array full of 'x's, hint seach numpy full\n", - "full = None\n", + "full = np.full((2,2), 'x')\n", "print(full, \"\\n\")\n", "\n", "# Create an array of the form [10, 15, 20] using np.arange\n", - "even = None\n", + "even = np.arange(10, 21, 5)\n", "print(even, \"\\n\")\n", "\n", "# Create an array of the form [0. 0.25 0.5 0.75 1. 1.25 1.5 1.75 2. ] using np.linspace\n", - "line = None\n", + "line = np.linspace(0, 2, num=9)\n", "print(line, \"\\n\")\n" ] }, @@ -172,7 +219,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -180,9 +227,19 @@ "id": "NSMJt_4x0qan", "outputId": "81e81c55-af1d-4149-dc7c-7edc8a1a582d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5 6 7]\n" + ] + } + ], "source": [ "arr1d = np.array([0,1, 2, 3, 4, 5, 6, 7, 8])\n", + "slice_arr1d = arr1d[5:8]\n", + "print(slice_arr1d)\n", "\n", "# using indexing select and output [5, 6, 7] from arr1d. Save this slice to a variable named slice_arr1d\n" ] @@ -196,7 +253,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 17, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -204,9 +261,21 @@ "id": "Xe-PXYhBp4bn", "outputId": "cf387d7a-d51f-4540-8d9a-571a9a24f7b5" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 0 1 2 3 4 5 12 12 12]\n", + "[ 5 12 12]\n" + ] + } + ], "source": [ - "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n" + "# Replace 6, 7, and 8 with 12 in arr1d using one line of code. Then print arr1d and slice_arr1d\n", + "arr1d[6:] = 12\n", + "print(arr1d)\n", + "print(slice_arr1d)\n" ] }, { @@ -218,7 +287,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -228,7 +297,8 @@ }, "outputs": [], "source": [ - "# Create a copy of arr1d and name it new_array. Hint: You can't use new_array = arr1d" + "# Create a copy of arr1d and name it new_array. Hint: You can't use new_array = arr1d\n", + "new_array = arr1d.copy()" ] }, { @@ -240,11 +310,24 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "3\n", + "3\n" + ] + } + ], "source": [ "arr2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n", + "print(arr2d[0][2])\n", + "print(arr2d[0, 2])\n", + "print(arr2d[-2][2])\n", "\n", "# There are two ways to select 3 from the 2 dimensional array above. Use both ways to select 3 and print the results. " ] @@ -258,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 20, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -266,16 +349,36 @@ "id": "kvMvyar216T5", "outputId": "943f42a4-469a-48f0-f4d4-546849ffa08d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[2 3 4]\n", + " [6 7 8]]\n", + "\n", + "\n", + "[[3]\n", + " [7]]\n", + "\n", + "\n", + "[[1 2 3 4]]\n" + ] + } + ], "source": [ "# In mult-dimensional arrays, slicing that omits later indices will return a lower-dimensional ndarray\n", "# Select and print out the following:\n", "\n", "# select first two rows and all but first column\n", - "\n", + "print(arr2d[:2, 1:])\n", + "print(\"\\n\")\n", "# select first two rows and only 3rd column\n", + "print(arr2d[:2, 2:3])\n", + "print(\"\\n\")\n", "\n", - "# select all columns in the first row\n" + "# select all columns in the first row\n", + "print(arr2d[:1, :4])" ] }, { @@ -298,7 +401,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -306,12 +409,37 @@ "id": "IMEp_RGh_Fu5", "outputId": "45332ef8-2c42-4c40-af09-6caaf98bca6b" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 0 0 1 1 1 1]\n", + " [0 1 1 0 1 1 0]\n", + " [1 1 0 0 0 0 1]\n", + " [1 1 1 0 0 1 0]\n", + " [0 1 0 0 0 1 1]\n", + " [0 1 1 1 0 0 1]\n", + " [1 1 1 1 1 0 0]]\n", + "['Bob' 'Joe' 'Will' 'Bob' 'Will' 'Joe' 'Joe']\n", + "\n", + "\n", + "[[1 0 0 1 1 1 1]\n", + " [1 1 1 0 0 1 0]]\n" + ] + } + ], "source": [ "import numpy as np\n", "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n", "data = np.random.randint(2, size=(7, 7)) # create an array of random values\n", + "print(data)\n", + "print(names)\n", + "\n", "\n", + " \n", + "print(\"\\n\")\n", + "print(data[names == \"Bob\"])\n", "# Select and print the first and forth rows from 'data' using just the array 'names' and 'data'. Print out both the selected rows and 'data'. \n", "# Hint\" The \"Bob\" appears in both the first and forth rows. " ] @@ -327,7 +455,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -335,11 +463,20 @@ "id": "BgULatpwt7X_", "outputId": "c31d49b4-92ff-4514-c91b-8eeb42573b5f" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 0 0 1 1 1 1]\n", + " [1 1 1 0 0 1 0]]\n" + ] + } + ], "source": [ "# Again output the first and fouth rows using the cond below. Meaning replace None with an expression\n", - "cond = None\n", - "data[cond]" + "cond = data[names == \"Bob\"]\n", + "print(cond)" ] }, { @@ -353,7 +490,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -361,9 +498,23 @@ "id": "EP-SDDVIvK8s", "outputId": "cce4fab9-cd5e-46c2-c615-f2518f1b4a72" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0 1 1 0 1 1 0]\n", + " [1 1 0 0 0 0 1]\n", + " [0 1 0 0 0 1 1]\n", + " [0 1 1 1 0 0 1]\n", + " [1 1 1 1 1 0 0]]\n" + ] + } + ], "source": [ - "# Select all rows except the first and forth" + "# Select all rows except the first and forth\n", + "negatedData = data[~(names == \"Bob\")]\n", + "print(negatedData)" ] }, { @@ -377,7 +528,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -385,9 +536,20 @@ "id": "QYjmF7eFvTmc", "outputId": "172adeb5-31d7-47ad-e3af-59c352f96f97" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 1 1 1]\n", + "[1 0 0 1 0]\n" + ] + } + ], "source": [ - "# Select the last 5 columns for the first and forth row" + "# Select the last 5 columns for the first and forth row\n", + "print(data[0, 2:])\n", + "print(data[3, 2:])" ] }, { @@ -401,7 +563,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -409,9 +571,22 @@ "id": "sciz0aprvh8u", "outputId": "24c8c092-cbe9-4665-9e01-9d193bcac8df" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1 0 0 1 1 1 1]\n", + " [1 1 0 0 0 0 1]\n", + " [1 1 1 0 0 1 0]\n", + " [0 1 0 0 0 1 1]]\n" + ] + } + ], "source": [ - "# Select the first, third, forth and fifth row using similar conditioins and an OR operator" + "# Select the first, third, forth and fifth row using similar conditioins and an OR operator\n", + "cond2 = (names == \"Bob\") | (names == \"Will\")\n", + "print(data[cond2])" ] }, { @@ -436,7 +611,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -444,18 +619,36 @@ "id": "HdWTjc8CZI5R", "outputId": "e070b71e-bdee-4abc-f378-474978208ad5" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 2. 3.]\n", + " [4. 5. 6.]]\n", + "\n", + "\n", + "[[ 6. 7. 8.]\n", + " [ 9. 10. 11.]]\n", + "\n", + "\n", + "[[10. 20. 30.]\n", + " [40. 50. 60.]]\n" + ] + } + ], "source": [ "array1 = np.array([[1., 2., 3.], [4., 5., 6.]])\n", "\n", "print(array1)\n", + "print(\"\\n\")\n", "\n", "# Add 5 to all values in array1 and print \n", - "print()\n", - "\n", + "print(5 + array1)\n", + "print(\"\\n\")\n", "\n", "# Multiple all values in array1 by 10 and print \n", - "print()\n" + "print(10 * array1)\n" ] }, { @@ -469,7 +662,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -477,11 +670,20 @@ "id": "uHr0DxrmaOEZ", "outputId": "b0f74550-427d-4333-c010-45f886677379" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[False True False]\n", + " [ True False True]]\n" + ] + } + ], "source": [ "array2 = np.array([[0., 4., 1.], [7., 2., 12.]])\n", "\n", - "\n", + "print(array1 < array2)\n", "# Test to see if pairwise elements in array1 are less than the corresponding element in array2. Output the results.\n", "# i.e. compariing the first elements of the first rows would output False because 1 is not less than 0" ] @@ -519,7 +721,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -527,13 +729,23 @@ "id": "k5pIynI12dmY", "outputId": "cd39dc42-7bab-4a92-c3e0-151a287d0b07" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1. 0. 1. 1. 0.]\n" + ] + } + ], "source": [ "arr1 = np.ones(5)\n", "arr2 = np.zeros(5)\n", "cond = np.array([True, False, True, True, False])\n", "\n", - "# Output the value from `arr1` whenever the corresponding value in `cond` is True, and otherwise take the value from `arr2`. \n" + "# Output the value from `arr1` whenever the corresponding value in `cond` is True, and otherwise take the value from `arr2`. \n", + "answer = np.where(cond == 1, arr1, arr2)\n", + "print(answer)" ] }, { @@ -572,12 +784,34 @@ "id": "ImvBT7NX3aY7", "outputId": "d04b3b76-91e4-4a40-e047-1e7a55bfd4ed" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[3 2 0 1]\n", + " [2 1 3 3]\n", + " [1 1 1 3]\n", + " [0 2 3 0]\n", + " [1 0 3 2]]\n", + "\n", + "\n", + "the mean is 1.6\n", + "the sum is 32\n", + "(5, 4)\n" + ] + } + ], "source": [ "arr = np.random.randint(4, size=(5, 4))\n", "print(arr)\n", "\n", - "# Calculate and print the mean and sum for all values in the array\n" + "# Calculate and print the mean and sum for all values in the array\n", + "mean = np.mean(arr)\n", + "sum = np.sum(arr)\n", + "print(\"\\n\")\n", + "print(f\"the mean is {mean}\")\n", + "print(f\"the sum is {sum}\")" ] }, { @@ -589,7 +823,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -597,9 +831,33 @@ "id": "kxw7AUVm31Ea", "outputId": "b1cb6ece-5056-41b1-8c1b-f57612b9dae1" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2. 1.75 1.25 1.5 1.5 ]\n", + "[8 7 5 6 6]\n", + "[2. 1.2 1.8 1.4]\n", + "[10 6 9 7]\n" + ] + } + ], "source": [ - "# Calculate the mean and sum across the columns and the rows. Print out both\n" + "# Calculate the mean and sum across the columns and the rows. Print out both\n", + "\n", + "\n", + "mean_of_rows = np.mean(arr, axis=1)\n", + "sum_of_rows = np.sum(arr, axis=1)\n", + "\n", + "mean_of_columns = np.mean(arr, axis=0)\n", + "sum_of_columns = np.sum(arr, axis=0)\n", + "\n", + "print(mean_of_rows)\n", + "print(sum_of_rows)\n", + "\n", + "print(mean_of_columns)\n", + "print(sum_of_columns)\n" ] }, { @@ -611,15 +869,42 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "full array: [1 2 3 4 5 6]\n", + "\n", + "\n", + "columns [[1 1 1]\n", + " [2 2 2]]\n", + "\n", + "\n", + "rows: [[1 2 3]\n", + " [1 2 3]]\n" + ] + } + ], "source": [ + "arr6 = np.array([[1, 1, 1], [1, 1, 1]])\n", "# Print out the cumulative sum for the full array, the columns and the rows. \n", "# For example the array [[1, 1, 1], [1, 1, 1]] would have the following outputs:\n", "# full array: [1, 2, 3, 4, 5, 6]\n", + "rows = arr6.cumsum(axis=1)\n", + "columns = arr6.cumsum(axis=0)\n", + "fullarray = arr6.cumsum()\n", + "\n", "# columns: [[1, 1, 1], [2, 2, 2]]\n", - "# rows: [[1, 2, 3], [1, 2, 3]]\n" + "# rows: [[1, 2, 3], [1, 2, 3]]\n", + "print(f\"full array: {fullarray}\")\n", + "print(\"\\n\")\n", + "print(f\"columns {columns}\")\n", + "print(\"\\n\")\n", + "print(f\"rows: {rows}\")\n", + "\n" ] }, { @@ -631,7 +916,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" @@ -639,7 +924,17 @@ "id": "sDnJpvUR1B1W", "outputId": "c4806b83-0ba9-448e-c4b7-f9ca53a8900d" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 1 2 3 4 5 6 7 8 9]\n", + "[1 3 5 7 9]\n", + "[9 8 7 6 5 4 3 2 1 0]\n" + ] + } + ], "source": [ "\n", "arr = np.arange(10)\n", @@ -647,8 +942,12 @@ "\n", "\n", "# Print out all all odd values\n", + "odd_numbers = arr[arr % 2 == 1]\n", + "print(odd_numbers)\n", "\n", - "# Print out all values in reverse order\n" + "# Print out all values in reverse order\n", + "reverse_array = arr[::-1]\n", + "print(reverse_array)" ] } ], @@ -678,7 +977,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.13.5" } }, "nbformat": 4, diff --git a/week_4_panadas.ipynb b/week_4_panadas.ipynb index 9080a02..f0fa221 100644 --- a/week_4_panadas.ipynb +++ b/week_4_panadas.ipynb @@ -52,10 +52,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 1\n", + "1 2\n", + "2 3\n", + "3 4\n", + "dtype: int64\n", + "\n", + "[np.int64(1), np.int64(2), np.int64(3), np.int64(4)]\n", + "Length: 4, dtype: int64\n", + "RangeIndex(start=0, stop=4, step=1)\n" + ] + } + ], + "source": [ + "pandas_series = pd.Series([1, 2, 3, 4])\n", + "print(pandas_series)\n", + "print(pandas_series.array)\n", + "print(pandas_series.index)" + ] }, { "cell_type": "markdown", @@ -66,10 +87,21 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "series2 = pd.Series([1, 2, 3, 4], index = [\"a\", \"b\", \"c\", \"d\"])\n", + "print(series2[\"a\"])" + ] }, { "cell_type": "markdown", @@ -85,10 +117,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Ohio 35000\n", + "Texas 71000\n", + "Oregon 16000\n", + "Utah 5000\n", + "dtype: int64\n" + ] + } + ], + "source": [ + "states_dict = {\"Ohio\": 35000, \"Texas\": 71000,\n", + " \"Oregon\": 16000, \"Utah\":5000}\n", + "states_series = pd.Series(states_dict)\n", + "print(states_series) " + ] }, { "cell_type": "markdown", @@ -99,10 +148,25 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OH 35000\n", + "TX 71000\n", + "OR 16000\n", + "UT 5000\n", + "dtype: int64\n" + ] + } + ], + "source": [ + "states_series.index = [\"OH\", \"TX\", \"OR\", \"UT\"]\n", + "print(states_series)" + ] }, { "cell_type": "markdown", @@ -125,9 +189,91 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
stateyearpop
0Ohio20001.5
1Ohio20011.7
2Ohio20023.6
3Nevada20012.4
4Nevada20022.9
5Nevada20033.2
\n", + "
" + ], + "text/plain": [ + " state year pop\n", + "0 Ohio 2000 1.5\n", + "1 Ohio 2001 1.7\n", + "2 Ohio 2002 3.6\n", + "3 Nevada 2001 2.4\n", + "4 Nevada 2002 2.9\n", + "5 Nevada 2003 3.2" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "data = {\n", " 'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],\n", @@ -148,10 +294,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " state year pop\n", + "zero Ohio 2000 1.5\n", + "one Ohio 2001 1.7\n", + "two Ohio 2002 3.6\n", + "three Nevada 2001 2.4\n", + "four Nevada 2002 2.9\n", + "five Nevada 2003 3.2\n" + ] + } + ], + "source": [ + "df.index = [\"zero\", \"one\", \"two\", \"three\", \"four\", \"five\"]\n", + "print(df)" + ] }, { "cell_type": "markdown", @@ -162,10 +325,37 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zero 2000\n", + "one 2001\n", + "two 2002\n", + "three 2001\n", + "four 2002\n", + "five 2003\n", + "Name: year, dtype: int64\n", + "\n", + "\n", + "zero Ohio\n", + "one Ohio\n", + "two Ohio\n", + "three Nevada\n", + "four Nevada\n", + "five Nevada\n", + "Name: state, dtype: object\n" + ] + } + ], + "source": [ + "print(df[\"year\"])\n", + "print(\"\\n\")\n", + "print(df.iloc[:, 0])" + ] }, { "cell_type": "markdown", @@ -176,10 +366,28 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "state Ohio\n", + "year 2002\n", + "pop 3.6\n", + "Name: two, dtype: object\n", + "state Ohio\n", + "year 2002\n", + "pop 3.6\n", + "Name: two, dtype: object\n" + ] + } + ], + "source": [ + "print(df.loc[\"two\"])\n", + "print(df.iloc[2])" + ] }, { "cell_type": "markdown", @@ -190,10 +398,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " state year pop rating\n", + "zero Ohio 2000 1.5 5\n", + "one Ohio 2001 1.7 4\n", + "two Ohio 2002 3.6 3\n", + "three Nevada 2001 2.4 2\n", + "four Nevada 2002 2.9 1\n", + "five Nevada 2003 3.2 0\n" + ] + } + ], + "source": [ + "df[\"rating\"] = np.arange(5, -1, -1)\n", + "print(df)" + ] }, { "cell_type": "markdown", @@ -204,10 +429,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " state year pop rating nonsense\n", + "zero Ohio 2000 1.5 5 7.5\n", + "one Ohio 2001 1.7 4 6.8\n", + "two Ohio 2002 3.6 3 10.8\n", + "three Nevada 2001 2.4 2 4.8\n", + "four Nevada 2002 2.9 1 2.9\n", + "five Nevada 2003 3.2 0 0.0\n" + ] + } + ], + "source": [ + "df[\"nonsense\"] = df[\"rating\"] * df[\"pop\"]\n", + "print(df)" + ] }, { "cell_type": "markdown", @@ -224,10 +466,38 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " series_numerical series_zeros series_random\n", + "a 0 0.0 0.516706\n", + "b 1 0.0 0.742704\n", + "c 2 0.0 0.487931\n", + "d 3 0.0 0.460146\n", + "e 4 0.0 0.542536\n" + ] + } + ], + "source": [ + "numbers = np.array([0, 1, 2, 3, 4])\n", + "zeros = np.zeros(5)\n", + "random_nums = np.random.rand(5)\n", + "\n", + "\n", + "\n", + "\n", + "series_numerical = pd.Series(numbers, index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "series_zeros = pd.Series(zeros, index=[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "series_random = pd.Series(random_nums, index =[\"a\",\"b\",\"c\",\"d\",\"e\"])\n", + "numeric_df = pd.DataFrame({\"series_numerical\": series_numerical,\n", + " \"series_zeros\": series_zeros,\n", + " \"series_random\": series_random})\n", + "print(numeric_df)\n" + ] }, { "cell_type": "markdown", @@ -238,10 +508,76 @@ }, { "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
series_numerical0.0000001.0000002.000000
series_zeros0.0000000.0000000.000000
series_random0.5167060.7427040.487931
\n", + "
" + ], + "text/plain": [ + " numerical zeros random\n", + "series_numerical 0.000000 1.000000 2.000000\n", + "series_zeros 0.000000 0.000000 0.000000\n", + "series_random 0.516706 0.742704 0.487931" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df = numeric_df.T.loc[:,[\"a\", \"b\", \"c\" ]].rename(\n", + " columns={\"a\": \"numerical\", \"b\": \"zeros\", \"c\": \"random\"}\n", + ")\n", + "transposed_numeric_df" + ] }, { "cell_type": "markdown", @@ -252,10 +588,24 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeros random\n", + "series_numerical 0.000000 1.000000 2.000000\n", + "series_zeros 0.000000 0.000000 0.000000\n", + "series_random 0.516706 0.742704 0.487931\n" + ] + } + ], + "source": [ + "transposed_numeric_df.sort_values(by=\"random\", ascending = False)\n", + "print(transposed_numeric_df)" + ] }, { "cell_type": "markdown", @@ -266,10 +616,55 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandom
\n", + "
" + ], + "text/plain": [ + "Empty DataFrame\n", + "Columns: [numerical, zeros, random]\n", + "Index: []" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "greater_than_two = transposed_numeric_df[transposed_numeric_df[\"numerical\"] > 2]\n", + "greater_than_two" + ] }, { "cell_type": "markdown", @@ -280,10 +675,69 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " numerical zeros random random_5\n", + "series_numerical 0.000000 1.000000 2.000000 10.000000\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000\n", + "series_random 0.516706 0.742704 0.487931 2.439657\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom_5
\n", + "
" + ], + "text/plain": [ + "Empty DataFrame\n", + "Columns: [numerical, zeros, random, random_5]\n", + "Index: []" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df[\"random_5\"] = transposed_numeric_df[\"random\"] * 5 \n", + "print(transposed_numeric_df)\n", + "greater_than_random_5 = transposed_numeric_df[transposed_numeric_df[\"numerical\"] > transposed_numeric_df[\"random_5\"]]\n", + "\n", + "greater_than_random_5" + ] }, { "cell_type": "markdown", @@ -294,10 +748,84 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom_5even
series_numerical0.0000001.0000002.00000010.000000True
series_zeros0.0000000.0000000.0000000.000000True
series_random0.5167060.7427040.4879312.439657False
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random_5 even\n", + "series_numerical 0.000000 1.000000 2.000000 10.000000 True\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 True\n", + "series_random 0.516706 0.742704 0.487931 2.439657 False" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df[\"even\"] = transposed_numeric_df[\"numerical\"] % 2 == 0\n", + "\n", + "\n", + "transposed_numeric_df" + ] }, { "cell_type": "markdown", @@ -308,10 +836,86 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
numericalzerosrandomrandom_5eveneven_odd
series_numerical0.0000001.0000002.00000010.000000Trueeven
series_zeros0.0000000.0000000.0000000.000000Trueeven
series_random0.5167060.7427040.4879312.439657Falseodd
\n", + "
" + ], + "text/plain": [ + " numerical zeros random random_5 even even_odd\n", + "series_numerical 0.000000 1.000000 2.000000 10.000000 True even\n", + "series_zeros 0.000000 0.000000 0.000000 0.000000 True even\n", + "series_random 0.516706 0.742704 0.487931 2.439657 False odd" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df[\"even_odd\"] = np.where(transposed_numeric_df[\"numerical\"] % 2 != 0, \"odd\", \"even\")\n", + "transposed_numeric_df" + ] }, { "cell_type": "markdown", @@ -322,10 +926,27 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "numerical 0.516706\n", + "zeros 1.742704\n", + "random 2.487931\n", + "random_5 12.439657\n", + "even 2\n", + "even_odd evenevenodd\n", + "dtype: object\n" + ] + } + ], + "source": [ + "sum_columns = transposed_numeric_df.sum()\n", + "print(sum_columns)" + ] }, { "cell_type": "markdown", @@ -338,8 +959,21 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "'series_random'" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "transposed_numeric_df[\"numerical\"].idxmax()\n" + ] } ], "metadata": { @@ -358,7 +992,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.13.5" } }, "nbformat": 4,