diff --git a/docs/_toc.yml b/docs/_toc.yml index d7603a6..a83a487 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -68,6 +68,7 @@ parts: chapters: - file: pandas/intro_to_pandas - file: pandas/visual_intro_to_pandas + - file: pandas/pandas_exercises - caption: TensorFlow chapters: diff --git a/docs/numpy/numpy_exercises.ipynb b/docs/numpy/numpy_exercises.ipynb index 777aaa8..490fcbf 100644 --- a/docs/numpy/numpy_exercises.ipynb +++ b/docs/numpy/numpy_exercises.ipynb @@ -5,7 +5,7 @@ "id": "6293cc50", "metadata": {}, "source": [ - "# 100 NumPy Exercises" + "# NumPy Exercises" ] }, { @@ -566,1083 +566,6 @@ "metadata": {}, "outputs": [], "source": [] - }, - { - "cell_type": "markdown", - "id": "9f52a2ef", - "metadata": {}, - "source": [ - "34. How to get all the dates corresponding to the month of July 2016? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d6c34dec", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "4a64e059", - "metadata": {}, - "source": [ - "35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "49267f60", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "8a98c2a8", - "metadata": {}, - "source": [ - "36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "368130b3", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "5a2e3d85", - "metadata": {}, - "source": [ - "37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1d974e8f", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "9720e2d5", - "metadata": {}, - "source": [ - "38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e5dd8fc9", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "c6f2c912", - "metadata": {}, - "source": [ - "39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6aaa27d3", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "a36d6ee4", - "metadata": {}, - "source": [ - "40. Create a random vector of size 10 and sort it (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e589d216", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "0fb4ad68", - "metadata": {}, - "source": [ - "41. How to sum a small array faster than np.sum? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a1ba05a0", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "024ac365", - "metadata": {}, - "source": [ - "42. Consider two random array A and B, check if they are equal (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7db3846d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "c68d2401", - "metadata": {}, - "source": [ - "43. Make an array immutable (read-only) (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7828539d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "55df6280", - "metadata": {}, - "source": [ - "44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f1c86ea0", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "2f60f1ec", - "metadata": {}, - "source": [ - "45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d9f48f1c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "084e8ad4", - "metadata": {}, - "source": [ - "46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "58b47c13", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "d7b9bc42", - "metadata": {}, - "source": [ - "47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a24824fb", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "65ebc11b", - "metadata": {}, - "source": [ - "48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e45d38fb", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "9b317a98", - "metadata": {}, - "source": [ - "49. How to print all the values of an array? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "309cdeb9", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "b580f9d0", - "metadata": {}, - "source": [ - "50. How to find the closest value (to a given scalar) in a vector? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ebfb6714", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "602e6252", - "metadata": {}, - "source": [ - "51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cf002b3a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "78bcd92f", - "metadata": {}, - "source": [ - "52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fea7f9a6", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "343c5a85", - "metadata": {}, - "source": [ - "53. How to convert a float (32 bits) array into an integer (32 bits) in place?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fc1b687b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "a69e1ace", - "metadata": {}, - "source": [ - "54. How to read the following file? (★★☆)\n", - "```\n", - "1, 2, 3, 4, 5\n", - "6, , , 7, 8\n", - " , , 9,10,11\n", - "```" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "1c7957d4", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "c23bd80d", - "metadata": {}, - "source": [ - "55. What is the equivalent of enumerate for numpy arrays? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d95d9578", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "2153a5ec", - "metadata": {}, - "source": [ - "56. Generate a generic 2D Gaussian-like array (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "61122d1e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "2e76d589", - "metadata": {}, - "source": [ - "57. How to randomly place p elements in a 2D array? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0c2e4fcc", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "8b3933eb", - "metadata": {}, - "source": [ - "58. Subtract the mean of each row of a matrix (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a306c9ef", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "ea8807c0", - "metadata": {}, - "source": [ - "59. How to sort an array by the nth column? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cc89a699", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "00fa00a1", - "metadata": {}, - "source": [ - "60. How to tell if a given 2D array has null columns? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3436e165", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "5a65c7a2", - "metadata": {}, - "source": [ - "61. Find the nearest value from a given value in an array (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "73f149d6", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "dd83e8af", - "metadata": {}, - "source": [ - "62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "29a709a1", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "6c042efa", - "metadata": {}, - "source": [ - "63. Create an array class that has a name attribute (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "45b3d2e0", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "0a075c3d", - "metadata": {}, - "source": [ - "64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "36c50470", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "0fb2f0c5", - "metadata": {}, - "source": [ - "65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "58aa5f82", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "d89dc506", - "metadata": {}, - "source": [ - "66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8fb43a49", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "535a5d81", - "metadata": {}, - "source": [ - "67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f954ab9d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "eee66d3c", - "metadata": {}, - "source": [ - "68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8929773a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "f90d4c40", - "metadata": {}, - "source": [ - "69. How to get the diagonal of a dot product? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b47d392d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "a8a97e7d", - "metadata": {}, - "source": [ - "70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ee6c6c37", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "d6a30d60", - "metadata": {}, - "source": [ - "71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "e9703f2e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "bac359a9", - "metadata": {}, - "source": [ - "72. How to swap two rows of an array? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f1b432b1", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "620c9f83", - "metadata": {}, - "source": [ - "73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4e595ffc", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "a76c55a2", - "metadata": {}, - "source": [ - "74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2d2ae65a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "d550c8e2", - "metadata": {}, - "source": [ - "75. How to compute averages using a sliding window over an array? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "841c457c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "6b170b00", - "metadata": {}, - "source": [ - "76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d9a0cf4e", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "e94a5c1d", - "metadata": {}, - "source": [ - "77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c0f9504f", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "4eb8b5c1", - "metadata": {}, - "source": [ - "78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "487f47ec", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "3df99948", - "metadata": {}, - "source": [ - "79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "dc2df37b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "11e972c2", - "metadata": {}, - "source": [ - "80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0291247f", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "93cfeab3", - "metadata": {}, - "source": [ - "81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "360de791", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "4c15ceff", - "metadata": {}, - "source": [ - "82. Compute a matrix rank (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9a4272d2", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "1171cb84", - "metadata": {}, - "source": [ - "83. How to find the most frequent value in an array?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "33e951c8", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "04f472aa", - "metadata": {}, - "source": [ - "84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7aade14a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "52836346", - "metadata": {}, - "source": [ - "85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4df70318", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "78c43ee5", - "metadata": {}, - "source": [ - "86. Consider a set of p matrices with shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "32fd2c3b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "158f9945", - "metadata": {}, - "source": [ - "87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5a9b808f", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "2e0ad860", - "metadata": {}, - "source": [ - "88. How to implement the Game of Life using numpy arrays? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "574acff5", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "69a892fa", - "metadata": {}, - "source": [ - "89. How to get the n largest values of an array (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "21b2bd48", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "b55ccacc", - "metadata": {}, - "source": [ - "90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4b645da4", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "89187a23", - "metadata": {}, - "source": [ - "91. How to create a record array from a regular array? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "da94c71a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "aa70d4ca", - "metadata": {}, - "source": [ - "92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7cff1bf5", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "5f79dd00", - "metadata": {}, - "source": [ - "93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "20c105b2", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "a80570cd", - "metadata": {}, - "source": [ - "94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "84db8de1", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "ba154f43", - "metadata": {}, - "source": [ - "95. Convert a vector of ints into a matrix binary representation (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "cfa96e1b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "420e2343", - "metadata": {}, - "source": [ - "96. Given a two dimensional array, how to extract unique rows? (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4680dfd0", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "89fef2f3", - "metadata": {}, - "source": [ - "97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bb9c14b2", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "7ad5cc5f", - "metadata": {}, - "source": [ - "98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0899d1d5", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "2b69272e", - "metadata": {}, - "source": [ - "99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "55c8b034", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "59ab5516", - "metadata": {}, - "source": [ - "100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "450877d8", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/docs/pandas/pandas_exercises.ipynb b/docs/pandas/pandas_exercises.ipynb new file mode 100644 index 0000000..fdff5a9 --- /dev/null +++ b/docs/pandas/pandas_exercises.ipynb @@ -0,0 +1,614 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Pandas Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Importing pandas\n", + "\n", + "### Getting started and checking your pandas setup\n", + "\n", + "Difficulty: *easy* \n", + "\n", + "**1.** Import pandas under the alias `pd`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**2.** Print the version of pandas that has been imported." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**3.** Print out all the *version* information of the libraries that are required by the pandas library." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## DataFrame basics\n", + "\n", + "### A few of the fundamental routines for selecting, sorting, adding and aggregating data in DataFrames\n", + "\n", + "Difficulty: *easy*\n", + "\n", + "Note: remember to import numpy using:\n", + "```python\n", + "import numpy as np\n", + "```\n", + "\n", + "Consider the following Python dictionary `data` and Python list `labels`:\n", + "\n", + "``` python\n", + "data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],\n", + " 'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],\n", + " 'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n", + " 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}\n", + "\n", + "labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n", + "```\n", + "(This is just some meaningless data I made up with the theme of animals and trips to a vet.)\n", + "\n", + "**4.** Create a DataFrame `df` from this dictionary `data` which has the index `labels`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "\n", + "data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],\n", + " 'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],\n", + " 'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],\n", + " 'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}\n", + "\n", + "labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n", + "\n", + "df = # (complete this line of code)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**5.** Display a summary of the basic information about this DataFrame and its data (*hint: there is a single method that can be called on the DataFrame*)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**6.** Return the first 3 rows of the DataFrame `df`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**7.** Select just the 'animal' and 'age' columns from the DataFrame `df`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**8.** Select the data in rows `[3, 4, 8]` *and* in columns `['animal', 'age']`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**9.** Select only the rows where the number of visits is greater than 3." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**10.** Select the rows where the age is missing, i.e. it is `NaN`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**11.** Select the rows where the animal is a cat *and* the age is less than 3." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**12.** Select the rows the age is between 2 and 4 (inclusive)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**13.** Change the age in row 'f' to 1.5." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**14.** Calculate the sum of all visits in `df` (i.e. find the total number of visits)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**15.** Calculate the mean age for each different animal in `df`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**16.** Append a new row 'k' to `df` with your choice of values for each column. Then delete that row to return the original DataFrame." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**17.** Count the number of each type of animal in `df`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**18.** Sort `df` first by the values in the 'age' in *decending* order, then by the value in the 'visits' column in *ascending* order (so row `i` should be first, and row `d` should be last)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**19.** The 'priority' column contains the values 'yes' and 'no'. Replace this column with a column of boolean values: 'yes' should be `True` and 'no' should be `False`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**20.** In the 'animal' column, change the 'snake' entries to 'python'." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**21.** For each animal type and each number of visits, find the mean age. In other words, each row is an animal, each column is a number of visits and the values are the mean ages (*hint: use a pivot table*)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## DataFrames: beyond the basics\n", + "\n", + "### Slightly trickier: you may need to combine two or more methods to get the right answer\n", + "\n", + "Difficulty: *medium*\n", + "\n", + "The previous section was tour through some basic but essential DataFrame operations. Below are some ways that you might need to cut your data, but for which there is no single \"out of the box\" method." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**22.** You have a DataFrame `df` with a column 'A' of integers. For example:\n", + "```python\n", + "df = pd.DataFrame({'A': [1, 2, 2, 3, 4, 5, 5, 5, 6, 7, 7]})\n", + "```\n", + "\n", + "How do you filter out rows which contain the same integer as the row immediately above?\n", + "\n", + "You should be left with a column containing the following values:\n", + "\n", + "```python\n", + "1, 2, 3, 4, 5, 6, 7\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**23.** Given a DataFrame of numeric values, say\n", + "```python\n", + "df = pd.DataFrame(np.random.random(size=(5, 3))) # a 5x3 frame of float values\n", + "```\n", + "\n", + "how do you subtract the row mean from each element in the row?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**24.** Suppose you have DataFrame with 10 columns of real numbers, for example:\n", + "\n", + "```python\n", + "df = pd.DataFrame(np.random.random(size=(5, 10)), columns=list('abcdefghij'))\n", + "```\n", + "Which column of numbers has the smallest sum? Return that column's label." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**25.** How do you count how many unique rows a DataFrame has (i.e. ignore all rows that are duplicates)? As input, use a DataFrame of zeros and ones with 10 rows and 3 columns.\n", + "\n", + "```python\n", + "df = pd.DataFrame(np.random.randint(0, 2, size=(10, 3)))\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The next three puzzles are slightly harder.\n", + "\n", + "\n", + "**26.** In the cell below, you have a DataFrame `df` that consists of 10 columns of floating-point numbers. Exactly 5 entries in each row are NaN values. \n", + "\n", + "For each row of the DataFrame, find the *column* which contains the *third* NaN value.\n", + "\n", + "You should return a Series of column labels: `e, c, d, h, d`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "nan = np.nan\n", + "\n", + "data = [[0.04, nan, nan, 0.25, nan, 0.43, 0.71, 0.51, nan, nan],\n", + " [ nan, nan, nan, 0.04, 0.76, nan, nan, 0.67, 0.76, 0.16],\n", + " [ nan, nan, 0.5 , nan, 0.31, 0.4 , nan, nan, 0.24, 0.01],\n", + " [0.49, nan, nan, 0.62, 0.73, 0.26, 0.85, nan, nan, nan],\n", + " [ nan, nan, 0.41, nan, 0.05, nan, 0.61, nan, 0.48, 0.68]]\n", + "\n", + "columns = list('abcdefghij')\n", + "\n", + "df = pd.DataFrame(data, columns=columns)\n", + "\n", + "# write a solution to the question here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**27.** A DataFrame has a column of groups 'grps' and and column of integer values 'vals': \n", + "\n", + "```python\n", + "df = pd.DataFrame({'grps': list('aaabbcaabcccbbc'), \n", + " 'vals': [12,345,3,1,45,14,4,52,54,23,235,21,57,3,87]})\n", + "```\n", + "For each *group*, find the sum of the three greatest values. You should end up with the answer as follows:\n", + "```\n", + "grps\n", + "a 409\n", + "b 156\n", + "c 345\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df = pd.DataFrame({'grps': list('aaabbcaabcccbbc'),\n", + " 'vals': [12,345,3,1,45,14,4,52,54,23,235,21,57,3,87]})\n", + "\n", + "# write a solution to the question here" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**28.** The DataFrame `df` constructed below has two integer columns 'A' and 'B'. The values in 'A' are between 1 and 100 (inclusive). \n", + "\n", + "For each group of 10 consecutive integers in 'A' (i.e. `(0, 10]`, `(10, 20]`, ...), calculate the sum of the corresponding values in column 'B'.\n", + "\n", + "The answer should be a Series as follows:\n", + "\n", + "```\n", + "A\n", + "(0, 10] 635\n", + "(10, 20] 360\n", + "(20, 30] 315\n", + "(30, 40] 306\n", + "(40, 50] 750\n", + "(50, 60] 284\n", + "(60, 70] 424\n", + "(70, 80] 526\n", + "(80, 90] 835\n", + "(90, 100] 852\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "df = pd.DataFrame(np.random.RandomState(8765).randint(1, 101, size=(100, 2)), columns = [\"A\", \"B\"])\n", + "\n", + "# write a solution to the question here" + ] + } + ], + "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.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}