diff --git a/your-code/.ipynb_checkpoints/main-checkpoint.ipynb b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..0204444 --- /dev/null +++ b/your-code/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,430 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# List Comprehensions Lab\n", + "\n", + "Complete the following set of exercises to solidify your knowledge of list comprehensions." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import numpy as np\n", + "import pandas as pd" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 1. Use a list comprehension to create and print a list of consecutive integers starting with 1 and ending with 50." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]\n" + ] + } + ], + "source": [ + "consecutive_integers = [num for num in range(1,51)]\n", + "print(consecutive_integers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. Use a list comprehension to create and print a list of even numbers starting with 2 and ending with 200." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n" + ] + } + ], + "source": [ + "even_numbers=[num for num in range(2,201,2)]\n", + "print(even_numbers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. Use a list comprehension to create and print a list containing all elements of the 10 x 4 Numpy array below." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "a = np.array([[0.84062117, 0.48006452, 0.7876326 , 0.77109654],\n", + " [0.44409793, 0.09014516, 0.81835917, 0.87645456],\n", + " [0.7066597 , 0.09610873, 0.41247947, 0.57433389],\n", + " [0.29960807, 0.42315023, 0.34452557, 0.4751035 ],\n", + " [0.17003563, 0.46843998, 0.92796258, 0.69814654],\n", + " [0.41290051, 0.19561071, 0.16284783, 0.97016248],\n", + " [0.71725408, 0.87702738, 0.31244595, 0.76615487],\n", + " [0.20754036, 0.57871812, 0.07214068, 0.40356048],\n", + " [0.12149553, 0.53222417, 0.9976855 , 0.12536346],\n", + " [0.80930099, 0.50962849, 0.94555126, 0.33364763]])" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.84062117, 0.48006452, 0.7876326, 0.77109654, 0.44409793, 0.09014516, 0.81835917, 0.87645456, 0.7066597, 0.09610873, 0.41247947, 0.57433389, 0.29960807, 0.42315023, 0.34452557, 0.4751035, 0.17003563, 0.46843998, 0.92796258, 0.69814654, 0.41290051, 0.19561071, 0.16284783, 0.97016248, 0.71725408, 0.87702738, 0.31244595, 0.76615487, 0.20754036, 0.57871812, 0.07214068, 0.40356048, 0.12149553, 0.53222417, 0.9976855, 0.12536346, 0.80930099, 0.50962849, 0.94555126, 0.33364763]\n" + ] + } + ], + "source": [ + "numpy_elements=[arr_2 for arr_1 in a for arr_2 in arr_1]\n", + "\n", + "print(numpy_elements)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4. Add a condition to the list comprehension above so that only values greater than or equal to 0.5 are printed." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "output=[ele for ele in numpy_elements if ele >= 0.5]\n", + "print(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5. Use a list comprehension to create and print a list containing all elements of the 5 x 2 x 3 Numpy array below." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "b = np.array([[[0.55867166, 0.06210792, 0.08147297],\n", + " [0.82579068, 0.91512478, 0.06833034]],\n", + "\n", + " [[0.05440634, 0.65857693, 0.30296619],\n", + " [0.06769833, 0.96031863, 0.51293743]],\n", + "\n", + " [[0.09143215, 0.71893382, 0.45850679],\n", + " [0.58256464, 0.59005654, 0.56266457]],\n", + "\n", + " [[0.71600294, 0.87392666, 0.11434044],\n", + " [0.8694668 , 0.65669313, 0.10708681]],\n", + "\n", + " [[0.07529684, 0.46470767, 0.47984544],\n", + " [0.65368638, 0.14901286, 0.23760688]]])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.55867166, 0.06210792, 0.08147297, 0.82579068, 0.91512478, 0.06833034, 0.05440634, 0.65857693, 0.30296619, 0.06769833, 0.96031863, 0.51293743, 0.09143215, 0.71893382, 0.45850679, 0.58256464, 0.59005654, 0.56266457, 0.71600294, 0.87392666, 0.11434044, 0.8694668, 0.65669313, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.65368638, 0.14901286, 0.23760688]\n" + ] + } + ], + "source": [ + "flattened_b=[arr_3 for arr_1 in b for arr_2 in arr_1 for arr_3 in arr_2]\n", + "print(flattened_b)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5. Add a condition to the list comprehension above so that the last value in each subarray is printed, but only if it is less than or equal to 0.5." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.08147297, 0.06833034, 0.30296619, 0.45850679, 0.11434044, 0.10708681, 0.47984544, 0.23760688]\n" + ] + } + ], + "source": [ + "flattened_subarray=[arr_3 for arr_1 in b for arr_2 in arr_1 for arr_3 in arr_2 if arr_3 <= 0.5 and arr_2[-1]==arr_3]\n", + "\n", + "print(flattened_subarray)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. Use a list comprehension to select and print the names of all CSV files in the */data* directory. \n", + "\n", + "https://dzone.com/articles/listing-a-directory-with-python#:~:text=The%20simplest%20way%20to%20get,current%20directory%20of%20the%20process.&text=As%20you%20can%20see%2C%20the,a%20file%2C%20directory%2C%20etc." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['sample_file_1.csv', 'sample_file_0.csv', 'sample_file_2.csv', 'sample_file_3.csv', 'sample_file_7.csv', 'sample_file_6.csv', 'sample_file_4.csv', 'sample_file_5.csv', 'sample_file_8.csv', 'sample_file_9.csv']\n" + ] + } + ], + "source": [ + "csv_names=[f for f in os.listdir(\"../data\") if f.endswith('.csv')]\n", + "print(csv_names)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 7. Use a list comprehension and the Pandas `read_csv` and `concat` methods to read all CSV files in the */data* directory and combine them into a single data frame. Display the top 10 rows of the resulting data frame." + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " 0 1 2 3 4 5 6 \\\n", + "0 0.276827 0.260054 0.942397 0.113187 0.781355 0.475740 0.152061 \n", + "1 0.995885 0.158381 0.244274 0.962163 0.651900 0.930665 0.577190 \n", + "2 0.641917 0.821055 0.392437 0.782617 0.510762 0.428320 0.017324 \n", + "3 0.806532 0.569258 0.148175 0.809987 0.459632 0.735762 0.730664 \n", + "4 0.311185 0.501165 0.365979 0.782807 0.776795 0.797199 0.791946 \n", + "0 0.734751 0.195362 0.734309 0.598184 0.763433 0.263434 0.868066 \n", + "1 0.772607 0.445391 0.249642 0.787922 0.598583 0.827238 0.624126 \n", + "2 0.226428 0.268764 0.694262 0.622335 0.063843 0.122683 0.815625 \n", + "3 0.362748 0.495430 0.113876 0.594149 0.612522 0.625204 0.864050 \n", + "4 0.033415 0.340433 0.464971 0.363737 0.025815 0.434129 0.415163 \n", + "\n", + " 7 8 9 10 11 12 13 \\\n", + "0 0.250324 0.147078 0.162984 0.977025 0.509619 0.593212 0.911839 \n", + "1 0.087914 0.960261 0.580840 0.194616 0.661459 0.674085 0.049326 \n", + "2 0.680720 0.340412 0.462513 0.785776 0.251949 0.032847 0.995700 \n", + "3 0.934502 0.080322 0.763502 0.398504 0.027637 0.409665 0.942846 \n", + "4 0.847157 0.771811 0.233944 0.522344 0.053030 0.208551 0.824354 \n", + "0 0.058092 0.753502 0.587513 0.311608 0.178356 0.182922 0.147631 \n", + "1 0.601524 0.688753 0.338870 0.081595 0.471474 0.267443 0.453351 \n", + "2 0.584542 0.032594 0.589775 0.764350 0.650973 0.565705 0.691784 \n", + "3 0.260279 0.528873 0.168043 0.715929 0.677014 0.175735 0.632370 \n", + "4 0.892210 0.381701 0.415264 0.790801 0.696930 0.819751 0.944029 \n", + "\n", + " 14 15 16 17 18 19 \n", + "0 0.257645 0.386457 0.696932 0.069162 0.952291 0.286542 \n", + "1 0.785803 0.315645 0.495355 0.232135 0.549324 0.572232 \n", + "2 0.816563 0.735692 0.435998 0.430411 0.531757 0.489528 \n", + "3 0.133256 0.157158 0.929446 0.402791 0.685976 0.246594 \n", + "4 0.588567 0.604341 0.232964 0.229109 0.022881 0.479022 \n", + "0 0.391188 0.816049 0.749068 0.293260 0.937828 0.880858 \n", + "1 0.800716 0.045749 0.683793 0.389789 0.016787 0.503695 \n", + "2 0.265223 0.739031 0.560394 0.334802 0.517694 0.646110 \n", + "3 0.926715 0.085675 0.120525 0.141746 0.771144 0.489660 \n", + "4 0.869965 0.041723 0.819140 0.676051 0.109349 0.872947 \n" + ] + } + ], + "source": [ + "data_sets = [pd.read_csv(os.path.join('../data', f)) for f in csv_names]\n", + "data = pd.concat(data_sets, axis=0)\n", + "print(type(data))\n", + "df=pd.DataFrame(data)\n", + "# print(df.iloc[0:11])\n", + "print(df.head(10))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 8. Use a list comprehension to select and print the column numbers for columns from the data set whose median is less than 0.48." + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12',\n", + " '13', '14', '15', '16', '17', '18', '19'],\n", + " dtype='object')\n", + " 2 9 12 14 17\n", + "0 0.942397 0.162984 0.593212 0.257645 0.069162\n", + "1 0.244274 0.580840 0.674085 0.785803 0.232135\n", + "2 0.392437 0.462513 0.032847 0.816563 0.430411\n", + "3 0.148175 0.763502 0.409665 0.133256 0.402791\n", + "4 0.365979 0.233944 0.208551 0.588567 0.229109\n", + "0 0.734309 0.587513 0.182922 0.391188 0.293260\n", + "1 0.249642 0.338870 0.267443 0.800716 0.389789\n", + "2 0.694262 0.589775 0.565705 0.265223 0.334802\n", + "3 0.113876 0.168043 0.175735 0.926715 0.141746\n", + "4 0.464971 0.415264 0.819751 0.869965 0.676051\n", + "0 0.918270 0.591727 0.177649 0.202404 0.159040\n", + "1 0.916655 0.586035 0.915342 0.489504 0.358977\n", + "2 0.954057 0.867511 0.552062 0.739656 0.602166\n", + "3 0.475459 0.410444 0.856437 0.326310 0.361873\n", + "4 0.336707 0.194299 0.531840 0.084884 0.437889\n", + "0 0.500995 0.037230 0.153783 0.330318 0.033474\n", + "1 0.152394 0.543442 0.580359 0.931264 0.509798\n", + "2 0.179986 0.181267 0.070744 0.948127 0.074394\n", + "3 0.681403 0.513575 0.082292 0.088025 0.515933\n", + "4 0.029834 0.956949 0.454788 0.095214 0.513342\n", + "0 0.735894 0.895810 0.617711 0.336094 0.856135\n", + "1 0.797730 0.674311 0.015081 0.132391 0.752866\n", + "2 0.892328 0.131970 0.034188 0.098536 0.655947\n", + "3 0.959355 0.105477 0.400774 0.844655 0.807400\n", + "4 0.736542 0.390542 0.366139 0.175482 0.904593\n", + "0 0.245083 0.783107 0.207432 0.349212 0.886644\n", + "1 0.467321 0.048724 0.721515 0.338366 0.773155\n", + "2 0.005691 0.847337 0.898464 0.443979 0.302786\n", + "3 0.833368 0.547769 0.931623 0.821099 0.183948\n", + "4 0.184239 0.212762 0.634487 0.394633 0.538712\n", + "0 0.596603 0.717817 0.394465 0.084206 0.267455\n", + "1 0.708804 0.737445 0.073603 0.798467 0.631497\n", + "2 0.567476 0.418311 0.234886 0.421263 0.726412\n", + "3 0.121713 0.360098 0.847880 0.490628 0.567280\n", + "4 0.553956 0.197302 0.473574 0.413186 0.351496\n", + "0 0.183148 0.080025 0.543498 0.960030 0.660811\n", + "1 0.802663 0.156926 0.940414 0.933377 0.407580\n", + "2 0.120652 0.048815 0.090451 0.192501 0.858956\n", + "3 0.476889 0.349850 0.841210 0.101031 0.149982\n", + "4 0.055405 0.449771 0.158077 0.730355 0.078734\n", + "0 0.673568 0.111557 0.446974 0.713369 0.982256\n", + "1 0.850528 0.892183 0.429496 0.283769 0.688337\n", + "2 0.807189 0.184300 0.573440 0.032852 0.719610\n", + "3 0.407187 0.684265 0.756593 0.551369 0.792803\n", + "4 0.392800 0.634608 0.682941 0.450950 0.221165\n", + "0 0.160848 0.882762 0.016789 0.984182 0.867894\n", + "1 0.089925 0.412141 0.176157 0.834378 0.602258\n", + "2 0.789796 0.633154 0.908288 0.462130 0.076819\n", + "3 0.935787 0.892383 0.846208 0.471880 0.665568\n", + "4 0.273505 0.922742 0.389451 0.129955 0.365442\n", + "['2', '9', '12', '14', '17']\n" + ] + } + ], + "source": [ + "print(df.columns)\n", + "print(df[columns])\n", + "columns = [column for column in df.columns if df[column].median() < 0.48]\n", + "print(columns)\n", + "# column_medians=[median for median in df]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 9. Use a list comprehension to add a new column (20) to the data frame whose values are the values in column 19 minus 0.1. Display the top 10 rows of the resulting data frame." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 10. Use a list comprehension to extract and print all values from the data set that are between 0.7 and 0.75." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/your-code/main.ipynb b/your-code/main.ipynb index c5931c4..6dfd9c6 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -29,10 +29,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]\n" + ] + } + ], + "source": [ + "consecutive_integers = [num for num in range(1,51)]\n", + "print(consecutive_integers)" + ] }, { "cell_type": "markdown", @@ -43,10 +54,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]\n" + ] + } + ], + "source": [ + "even_numbers=[num for num in range(2,201,2)]\n", + "print(even_numbers)" + ] }, { "cell_type": "markdown", @@ -57,7 +79,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -75,10 +97,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.84062117, 0.48006452, 0.7876326, 0.77109654, 0.44409793, 0.09014516, 0.81835917, 0.87645456, 0.7066597, 0.09610873, 0.41247947, 0.57433389, 0.29960807, 0.42315023, 0.34452557, 0.4751035, 0.17003563, 0.46843998, 0.92796258, 0.69814654, 0.41290051, 0.19561071, 0.16284783, 0.97016248, 0.71725408, 0.87702738, 0.31244595, 0.76615487, 0.20754036, 0.57871812, 0.07214068, 0.40356048, 0.12149553, 0.53222417, 0.9976855, 0.12536346, 0.80930099, 0.50962849, 0.94555126, 0.33364763]\n" + ] + } + ], + "source": [ + "numpy_elements=[arr_2 for arr_1 in a for arr_2 in arr_1]\n", + "\n", + "print(numpy_elements)\n" + ] }, { "cell_type": "markdown", @@ -89,10 +123,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.84062117, 0.7876326, 0.77109654, 0.81835917, 0.87645456, 0.7066597, 0.57433389, 0.92796258, 0.69814654, 0.97016248, 0.71725408, 0.87702738, 0.76615487, 0.57871812, 0.53222417, 0.9976855, 0.80930099, 0.50962849, 0.94555126]\n", + "[0.84062117, 0.7876326, 0.77109654, 0.81835917, 0.87645456, 0.7066597, 0.57433389, 0.92796258, 0.69814654, 0.97016248, 0.71725408, 0.87702738, 0.76615487, 0.57871812, 0.53222417, 0.9976855, 0.80930099, 0.50962849, 0.94555126]\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "output=[ele for ele in numpy_elements if ele >= 0.5]\n", + "print(output)\n", + "\n", + "output_2=[queso for jamon in a for queso in jamon if queso >= 0.5]\n", + "print(output_2)\n", + "\n", + "output== output_2" + ] }, { "cell_type": "markdown", @@ -103,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -125,10 +186,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.55867166, 0.06210792, 0.08147297, 0.82579068, 0.91512478, 0.06833034, 0.05440634, 0.65857693, 0.30296619, 0.06769833, 0.96031863, 0.51293743, 0.09143215, 0.71893382, 0.45850679, 0.58256464, 0.59005654, 0.56266457, 0.71600294, 0.87392666, 0.11434044, 0.8694668, 0.65669313, 0.10708681, 0.07529684, 0.46470767, 0.47984544, 0.65368638, 0.14901286, 0.23760688]\n" + ] + } + ], + "source": [ + "flattened_b=[arr_3 for arr_1 in b for arr_2 in arr_1 for arr_3 in arr_2]\n", + "print(flattened_b)" + ] }, { "cell_type": "markdown", @@ -139,24 +211,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.08147297, 0.06833034, 0.30296619, 0.45850679, 0.11434044, 0.10708681, 0.47984544, 0.23760688]\n" + ] + } + ], + "source": [ + "flattened_subarray=[arr_3 for arr_1 in b for arr_2 in arr_1 for arr_3 in arr_2 if arr_3 <= 0.5 and arr_2[-1]==arr_3]\n", + "\n", + "print(flattened_subarray)" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### 6. Use a list comprehension to select and print the names of all CSV files in the */data* directory." + "### 6. Use a list comprehension to select and print the names of all CSV files in the */data* directory. \n", + "\n", + "https://dzone.com/articles/listing-a-directory-with-python#:~:text=The%20simplest%20way%20to%20get,current%20directory%20of%20the%20process.&text=As%20you%20can%20see%2C%20the,a%20file%2C%20directory%2C%20etc." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['sample_file_1.csv', 'sample_file_0.csv', 'sample_file_2.csv', 'sample_file_3.csv', 'sample_file_7.csv', 'sample_file_6.csv', 'sample_file_4.csv', 'sample_file_5.csv', 'sample_file_8.csv', 'sample_file_9.csv']\n" + ] + } + ], + "source": [ + "csv_names=[f for f in os.listdir(\"../data\") if f.endswith('.csv')]\n", + "print(csv_names)" + ] }, { "cell_type": "markdown", @@ -167,24 +264,92 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " 0 1 2 3 4 5 6 \\\n", + "0 0.276827 0.260054 0.942397 0.113187 0.781355 0.475740 0.152061 \n", + "1 0.995885 0.158381 0.244274 0.962163 0.651900 0.930665 0.577190 \n", + "2 0.641917 0.821055 0.392437 0.782617 0.510762 0.428320 0.017324 \n", + "3 0.806532 0.569258 0.148175 0.809987 0.459632 0.735762 0.730664 \n", + "4 0.311185 0.501165 0.365979 0.782807 0.776795 0.797199 0.791946 \n", + "0 0.734751 0.195362 0.734309 0.598184 0.763433 0.263434 0.868066 \n", + "1 0.772607 0.445391 0.249642 0.787922 0.598583 0.827238 0.624126 \n", + "2 0.226428 0.268764 0.694262 0.622335 0.063843 0.122683 0.815625 \n", + "3 0.362748 0.495430 0.113876 0.594149 0.612522 0.625204 0.864050 \n", + "4 0.033415 0.340433 0.464971 0.363737 0.025815 0.434129 0.415163 \n", + "\n", + " 7 8 9 10 11 12 13 \\\n", + "0 0.250324 0.147078 0.162984 0.977025 0.509619 0.593212 0.911839 \n", + "1 0.087914 0.960261 0.580840 0.194616 0.661459 0.674085 0.049326 \n", + "2 0.680720 0.340412 0.462513 0.785776 0.251949 0.032847 0.995700 \n", + "3 0.934502 0.080322 0.763502 0.398504 0.027637 0.409665 0.942846 \n", + "4 0.847157 0.771811 0.233944 0.522344 0.053030 0.208551 0.824354 \n", + "0 0.058092 0.753502 0.587513 0.311608 0.178356 0.182922 0.147631 \n", + "1 0.601524 0.688753 0.338870 0.081595 0.471474 0.267443 0.453351 \n", + "2 0.584542 0.032594 0.589775 0.764350 0.650973 0.565705 0.691784 \n", + "3 0.260279 0.528873 0.168043 0.715929 0.677014 0.175735 0.632370 \n", + "4 0.892210 0.381701 0.415264 0.790801 0.696930 0.819751 0.944029 \n", + "\n", + " 14 15 16 17 18 19 \n", + "0 0.257645 0.386457 0.696932 0.069162 0.952291 0.286542 \n", + "1 0.785803 0.315645 0.495355 0.232135 0.549324 0.572232 \n", + "2 0.816563 0.735692 0.435998 0.430411 0.531757 0.489528 \n", + "3 0.133256 0.157158 0.929446 0.402791 0.685976 0.246594 \n", + "4 0.588567 0.604341 0.232964 0.229109 0.022881 0.479022 \n", + "0 0.391188 0.816049 0.749068 0.293260 0.937828 0.880858 \n", + "1 0.800716 0.045749 0.683793 0.389789 0.016787 0.503695 \n", + "2 0.265223 0.739031 0.560394 0.334802 0.517694 0.646110 \n", + "3 0.926715 0.085675 0.120525 0.141746 0.771144 0.489660 \n", + "4 0.869965 0.041723 0.819140 0.676051 0.109349 0.872947 \n" + ] + } + ], + "source": [ + "data_sets = [pd.read_csv(os.path.join('../data', f)) for f in csv_names]\n", + "data = pd.concat(data_sets, axis=0)\n", + "print(type(data))\n", + "df=pd.DataFrame(data)\n", + "# print(df.iloc[0:11])\n", + "print(df.head(10))" + ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### 8. Use a list comprehension to select and print the column numbers for columns from the data set whose median is less than 0.48." + "### 8. Use a list comprehension to select and print the column numbers for columns from the data set whose median is less than 0.48.\n", + "https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.columns.html\n", + "https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.items.html?highlight=items#pandas.DataFrame.items\n", + "https://stackoverflow.com/questions/28218698/how-to-iterate-over-columns-of-pandas-dataframe-to-run-regression\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['2', '9', '12', '14', '17']\n" + ] + } + ], + "source": [ + "\n", + "columns = [column for column in df.columns if df[column].median()<.48]\n", + "print(columns)\n", + "\n", + "# columns_2 = [otro_algo for algo in df.items() for otro_algo in algo]\n", + "# print(columns_2)\n" + ] }, { "cell_type": "markdown", @@ -195,10 +360,348 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 88, "metadata": {}, - "outputs": [], - "source": [] + "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", + " \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", + " \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", + " \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", + " \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", + " \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", + "
0123456789...11121314151617181920
00.2768270.2600540.9423970.1131870.7813550.4757400.1520610.2503240.1470780.162984...0.5096190.5932120.9118390.2576450.3864570.6969320.0691620.9522910.2865420.186542
10.9958850.1583810.2442740.9621630.6519000.9306650.5771900.0879140.9602610.580840...0.6614590.6740850.0493260.7858030.3156450.4953550.2321350.5493240.5722320.472232
20.6419170.8210550.3924370.7826170.5107620.4283200.0173240.6807200.3404120.462513...0.2519490.0328470.9957000.8165630.7356920.4359980.4304110.5317570.4895280.389528
30.8065320.5692580.1481750.8099870.4596320.7357620.7306640.9345020.0803220.763502...0.0276370.4096650.9428460.1332560.1571580.9294460.4027910.6859760.2465940.146594
40.3111850.5011650.3659790.7828070.7767950.7971990.7919460.8471570.7718110.233944...0.0530300.2085510.8243540.5885670.6043410.2329640.2291090.0228810.4790220.379022
00.7347510.1953620.7343090.5981840.7634330.2634340.8680660.0580920.7535020.587513...0.1783560.1829220.1476310.3911880.8160490.7490680.2932600.9378280.8808580.780858
10.7726070.4453910.2496420.7879220.5985830.8272380.6241260.6015240.6887530.338870...0.4714740.2674430.4533510.8007160.0457490.6837930.3897890.0167870.5036950.403695
20.2264280.2687640.6942620.6223350.0638430.1226830.8156250.5845420.0325940.589775...0.6509730.5657050.6917840.2652230.7390310.5603940.3348020.5176940.6461100.546110
30.3627480.4954300.1138760.5941490.6125220.6252040.8640500.2602790.5288730.168043...0.6770140.1757350.6323700.9267150.0856750.1205250.1417460.7711440.4896600.389660
40.0334150.3404330.4649710.3637370.0258150.4341290.4151630.8922100.3817010.415264...0.6969300.8197510.9440290.8699650.0417230.8191400.6760510.1093490.8729470.772947
\n", + "

10 rows × 21 columns

\n", + "
" + ], + "text/plain": [ + " 0 1 2 3 4 5 6 \\\n", + "0 0.276827 0.260054 0.942397 0.113187 0.781355 0.475740 0.152061 \n", + "1 0.995885 0.158381 0.244274 0.962163 0.651900 0.930665 0.577190 \n", + "2 0.641917 0.821055 0.392437 0.782617 0.510762 0.428320 0.017324 \n", + "3 0.806532 0.569258 0.148175 0.809987 0.459632 0.735762 0.730664 \n", + "4 0.311185 0.501165 0.365979 0.782807 0.776795 0.797199 0.791946 \n", + "0 0.734751 0.195362 0.734309 0.598184 0.763433 0.263434 0.868066 \n", + "1 0.772607 0.445391 0.249642 0.787922 0.598583 0.827238 0.624126 \n", + "2 0.226428 0.268764 0.694262 0.622335 0.063843 0.122683 0.815625 \n", + "3 0.362748 0.495430 0.113876 0.594149 0.612522 0.625204 0.864050 \n", + "4 0.033415 0.340433 0.464971 0.363737 0.025815 0.434129 0.415163 \n", + "\n", + " 7 8 9 ... 11 12 13 14 \\\n", + "0 0.250324 0.147078 0.162984 ... 0.509619 0.593212 0.911839 0.257645 \n", + "1 0.087914 0.960261 0.580840 ... 0.661459 0.674085 0.049326 0.785803 \n", + "2 0.680720 0.340412 0.462513 ... 0.251949 0.032847 0.995700 0.816563 \n", + "3 0.934502 0.080322 0.763502 ... 0.027637 0.409665 0.942846 0.133256 \n", + "4 0.847157 0.771811 0.233944 ... 0.053030 0.208551 0.824354 0.588567 \n", + "0 0.058092 0.753502 0.587513 ... 0.178356 0.182922 0.147631 0.391188 \n", + "1 0.601524 0.688753 0.338870 ... 0.471474 0.267443 0.453351 0.800716 \n", + "2 0.584542 0.032594 0.589775 ... 0.650973 0.565705 0.691784 0.265223 \n", + "3 0.260279 0.528873 0.168043 ... 0.677014 0.175735 0.632370 0.926715 \n", + "4 0.892210 0.381701 0.415264 ... 0.696930 0.819751 0.944029 0.869965 \n", + "\n", + " 15 16 17 18 19 20 \n", + "0 0.386457 0.696932 0.069162 0.952291 0.286542 0.186542 \n", + "1 0.315645 0.495355 0.232135 0.549324 0.572232 0.472232 \n", + "2 0.735692 0.435998 0.430411 0.531757 0.489528 0.389528 \n", + "3 0.157158 0.929446 0.402791 0.685976 0.246594 0.146594 \n", + "4 0.604341 0.232964 0.229109 0.022881 0.479022 0.379022 \n", + "0 0.816049 0.749068 0.293260 0.937828 0.880858 0.780858 \n", + "1 0.045749 0.683793 0.389789 0.016787 0.503695 0.403695 \n", + "2 0.739031 0.560394 0.334802 0.517694 0.646110 0.546110 \n", + "3 0.085675 0.120525 0.141746 0.771144 0.489660 0.389660 \n", + "4 0.041723 0.819140 0.676051 0.109349 0.872947 0.772947 \n", + "\n", + "[10 rows x 21 columns]" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['20'] = [jamon - .1 for jamon in df['19']]\n", + "df.head(10)" + ] }, { "cell_type": "markdown", @@ -209,15 +712,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 143, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.7356923324968598, 0.7357618655811269, 0.7306642125209889, 0.7347510852128797, 0.7343092314001137, 0.7490680818183477, 0.7390312927111312, 0.7159290265157912, 0.7033930409580709, 0.7013619954419494, 0.7396561066717461, 0.7045750112959754, 0.7358941798147411, 0.736029494090402, 0.7085098152301276, 0.7472962776291713, 0.7329024831531535, 0.7124169515124726, 0.71223975981198, 0.7365416322783028, 0.7025657075988269, 0.7298115760258881, 0.7294980584878026, 0.7215147068185102, 0.7130781095091867, 0.7393833032570581, 0.7258310816144985, 0.7383982036983279, 0.7178166705957928, 0.7088042362916818, 0.737444918174949, 0.7264121114538001, 0.7133695725698419, 0.7476323136755818, 0.726854508853197, 0.7111584953876777, 0.7008930873050235, 0.7209375615129192, 0.7303553716733908, 0.7274869207330491, 0.7133690799930356, 0.7078738356977186, 0.7032248108904495, 0.7196096371031628, 0.7244863338885654, 0.7321533684342264, 0.7376946446827494, 0.7132714209987683, 0.7023481768180954, 0.7051334608870933, 0.7280006345208285, 0.7150799992177622, 0.7321333453970806, 0.7173964318670598]\n" + ] + } + ], + "source": [ + "values = [value for numpy_arr in df.to_numpy() for value in numpy_arr if value > 0.7 and value < 0.75]\n", + "\n", + "print(values)" + ] + }, + { + "cell_type": "code", + "execution_count": 145, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0.27682695, 0.26005391, 0.942397 , ..., 0.95229148, 0.28654193,\n", + " 0.18654193],\n", + " [0.99588484, 0.15838114, 0.24427361, ..., 0.54932364, 0.57223234,\n", + " 0.47223234],\n", + " [0.64191662, 0.82105532, 0.39243713, ..., 0.53175724, 0.48952801,\n", + " 0.38952801],\n", + " ...,\n", + " [0.41375213, 0.69305179, 0.7897963 , ..., 0.79783604, 0.19759168,\n", + " 0.09759168],\n", + " [0.72800063, 0.34815596, 0.9357869 , ..., 0.93154222, 0.44812438,\n", + " 0.34812438],\n", + " [0.13494239, 0.87593106, 0.27350466, ..., 0.84781843, 0.58831904,\n", + " 0.48831904]])" + ] + }, + "execution_count": 145, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.apply()" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -231,7 +778,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.0" + "version": "3.9.7" } }, "nbformat": 4,