Format: application/octet-stream
⬇ Download File (10.2 KB)
   1  {
   2   "cells": [
   3    {
   4     "cell_type": "code",
   5     "execution_count": 1,
   6     "id": "00df6599",
   7     "metadata": {},
   8     "outputs": [],
   9     "source": [
  10      "import pandas as pd"
  11     ]
  12    },
  13    {
  14     "cell_type": "code",
  15     "execution_count": 2,
  16     "id": "e1ca1a42",
  17     "metadata": {},
  18     "outputs": [],
  19     "source": [
  20      "df= pd.read_csv('question 2.csv')"
  21     ]
  22    },
  23    {
  24     "cell_type": "code",
  25     "execution_count": 3,
  26     "id": "38250004",
  27     "metadata": {},
  28     "outputs": [
  29      {
  30       "name": "stdout",
  31       "output_type": "stream",
  32       "text": [
  33        "<class 'pandas.core.frame.DataFrame'>\n",
  34        "RangeIndex: 943 entries, 0 to 942\n",
  35        "Data columns (total 5 columns):\n",
  36        " #   Column      Non-Null Count  Dtype \n",
  37        "---  ------      --------------  ----- \n",
  38        " 0   user_id     943 non-null    int64 \n",
  39        " 1   age         943 non-null    int64 \n",
  40        " 2   gender      943 non-null    object\n",
  41        " 3   occupation  943 non-null    object\n",
  42        " 4   zip_code    943 non-null    object\n",
  43        "dtypes: int64(2), object(3)\n",
  44        "memory usage: 37.0+ KB\n"
  45       ]
  46      }
  47     ],
  48     "source": [
  49      "df.info()"
  50     ]
  51    },
  52    {
  53     "cell_type": "code",
  54     "execution_count": 4,
  55     "id": "7bc6d6ee",
  56     "metadata": {},
  57     "outputs": [
  58      {
  59       "data": {
  60        "text/html": [
  61         "<div>\n",
  62         "<style scoped>\n",
  63         "    .dataframe tbody tr th:only-of-type {\n",
  64         "        vertical-align: middle;\n",
  65         "    }\n",
  66         "\n",
  67         "    .dataframe tbody tr th {\n",
  68         "        vertical-align: top;\n",
  69         "    }\n",
  70         "\n",
  71         "    .dataframe thead th {\n",
  72         "        text-align: right;\n",
  73         "    }\n",
  74         "</style>\n",
  75         "<table border=\"1\" class=\"dataframe\">\n",
  76         "  <thead>\n",
  77         "    <tr style=\"text-align: right;\">\n",
  78         "      <th></th>\n",
  79         "      <th>user_id</th>\n",
  80         "      <th>age</th>\n",
  81         "      <th>gender</th>\n",
  82         "      <th>occupation</th>\n",
  83         "      <th>zip_code</th>\n",
  84         "    </tr>\n",
  85         "  </thead>\n",
  86         "  <tbody>\n",
  87         "    <tr>\n",
  88         "      <th>0</th>\n",
  89         "      <td>1</td>\n",
  90         "      <td>24</td>\n",
  91         "      <td>M</td>\n",
  92         "      <td>technician</td>\n",
  93         "      <td>85711</td>\n",
  94         "    </tr>\n",
  95         "    <tr>\n",
  96         "      <th>1</th>\n",
  97         "      <td>2</td>\n",
  98         "      <td>53</td>\n",
  99         "      <td>F</td>\n",
 100         "      <td>other</td>\n",
 101         "      <td>94043</td>\n",
 102         "    </tr>\n",
 103         "    <tr>\n",
 104         "      <th>2</th>\n",
 105         "      <td>3</td>\n",
 106         "      <td>23</td>\n",
 107         "      <td>M</td>\n",
 108         "      <td>writer</td>\n",
 109         "      <td>32067</td>\n",
 110         "    </tr>\n",
 111         "    <tr>\n",
 112         "      <th>3</th>\n",
 113         "      <td>4</td>\n",
 114         "      <td>24</td>\n",
 115         "      <td>M</td>\n",
 116         "      <td>technician</td>\n",
 117         "      <td>43537</td>\n",
 118         "    </tr>\n",
 119         "    <tr>\n",
 120         "      <th>4</th>\n",
 121         "      <td>5</td>\n",
 122         "      <td>33</td>\n",
 123         "      <td>F</td>\n",
 124         "      <td>other</td>\n",
 125         "      <td>15213</td>\n",
 126         "    </tr>\n",
 127         "  </tbody>\n",
 128         "</table>\n",
 129         "</div>"
 130        ],
 131        "text/plain": [
 132         "   user_id  age gender  occupation zip_code\n",
 133         "0        1   24      M  technician    85711\n",
 134         "1        2   53      F       other    94043\n",
 135         "2        3   23      M      writer    32067\n",
 136         "3        4   24      M  technician    43537\n",
 137         "4        5   33      F       other    15213"
 138        ]
 139       },
 140       "execution_count": 4,
 141       "metadata": {},
 142       "output_type": "execute_result"
 143      }
 144     ],
 145     "source": [
 146      "df.head()"
 147     ]
 148    },
 149    {
 150     "cell_type": "code",
 151     "execution_count": 5,
 152     "id": "01afb3dc",
 153     "metadata": {},
 154     "outputs": [],
 155     "source": [
 156      "min_age=df.groupby(df.occupation).min().age\n",
 157      "min_age.name= 'Min Age'"
 158     ]
 159    },
 160    {
 161     "cell_type": "code",
 162     "execution_count": 6,
 163     "id": "430ac10a",
 164     "metadata": {},
 165     "outputs": [],
 166     "source": [
 167      "max_age= df.groupby(df.occupation).max().age\n",
 168      "max_age.name= 'Max Age'"
 169     ]
 170    },
 171    {
 172     "cell_type": "code",
 173     "execution_count": 7,
 174     "id": "f3f6d4b0",
 175     "metadata": {},
 176     "outputs": [],
 177     "source": [
 178      "result= pd.DataFrame([min_age,max_age], columns=['Min Age', 'Max Age'])"
 179     ]
 180    },
 181    {
 182     "cell_type": "code",
 183     "execution_count": 8,
 184     "id": "8eb468a8",
 185     "metadata": {},
 186     "outputs": [
 187      {
 188       "data": {
 189        "text/html": [
 190         "<div>\n",
 191         "<style scoped>\n",
 192         "    .dataframe tbody tr th:only-of-type {\n",
 193         "        vertical-align: middle;\n",
 194         "    }\n",
 195         "\n",
 196         "    .dataframe tbody tr th {\n",
 197         "        vertical-align: top;\n",
 198         "    }\n",
 199         "\n",
 200         "    .dataframe thead th {\n",
 201         "        text-align: right;\n",
 202         "    }\n",
 203         "</style>\n",
 204         "<table border=\"1\" class=\"dataframe\">\n",
 205         "  <thead>\n",
 206         "    <tr style=\"text-align: right;\">\n",
 207         "      <th></th>\n",
 208         "      <th>Min Age</th>\n",
 209         "      <th>Max Age</th>\n",
 210         "    </tr>\n",
 211         "    <tr>\n",
 212         "      <th>occupation</th>\n",
 213         "      <th></th>\n",
 214         "      <th></th>\n",
 215         "    </tr>\n",
 216         "  </thead>\n",
 217         "  <tbody>\n",
 218         "    <tr>\n",
 219         "      <th>administrator</th>\n",
 220         "      <td>21</td>\n",
 221         "      <td>70</td>\n",
 222         "    </tr>\n",
 223         "    <tr>\n",
 224         "      <th>artist</th>\n",
 225         "      <td>19</td>\n",
 226         "      <td>48</td>\n",
 227         "    </tr>\n",
 228         "    <tr>\n",
 229         "      <th>doctor</th>\n",
 230         "      <td>28</td>\n",
 231         "      <td>64</td>\n",
 232         "    </tr>\n",
 233         "    <tr>\n",
 234         "      <th>educator</th>\n",
 235         "      <td>23</td>\n",
 236         "      <td>63</td>\n",
 237         "    </tr>\n",
 238         "    <tr>\n",
 239         "      <th>engineer</th>\n",
 240         "      <td>22</td>\n",
 241         "      <td>70</td>\n",
 242         "    </tr>\n",
 243         "    <tr>\n",
 244         "      <th>entertainment</th>\n",
 245         "      <td>15</td>\n",
 246         "      <td>50</td>\n",
 247         "    </tr>\n",
 248         "    <tr>\n",
 249         "      <th>executive</th>\n",
 250         "      <td>22</td>\n",
 251         "      <td>69</td>\n",
 252         "    </tr>\n",
 253         "    <tr>\n",
 254         "      <th>healthcare</th>\n",
 255         "      <td>22</td>\n",
 256         "      <td>62</td>\n",
 257         "    </tr>\n",
 258         "    <tr>\n",
 259         "      <th>homemaker</th>\n",
 260         "      <td>20</td>\n",
 261         "      <td>50</td>\n",
 262         "    </tr>\n",
 263         "    <tr>\n",
 264         "      <th>lawyer</th>\n",
 265         "      <td>21</td>\n",
 266         "      <td>53</td>\n",
 267         "    </tr>\n",
 268         "    <tr>\n",
 269         "      <th>librarian</th>\n",
 270         "      <td>23</td>\n",
 271         "      <td>69</td>\n",
 272         "    </tr>\n",
 273         "    <tr>\n",
 274         "      <th>marketing</th>\n",
 275         "      <td>24</td>\n",
 276         "      <td>55</td>\n",
 277         "    </tr>\n",
 278         "    <tr>\n",
 279         "      <th>none</th>\n",
 280         "      <td>11</td>\n",
 281         "      <td>55</td>\n",
 282         "    </tr>\n",
 283         "    <tr>\n",
 284         "      <th>other</th>\n",
 285         "      <td>13</td>\n",
 286         "      <td>64</td>\n",
 287         "    </tr>\n",
 288         "    <tr>\n",
 289         "      <th>programmer</th>\n",
 290         "      <td>20</td>\n",
 291         "      <td>63</td>\n",
 292         "    </tr>\n",
 293         "    <tr>\n",
 294         "      <th>retired</th>\n",
 295         "      <td>51</td>\n",
 296         "      <td>73</td>\n",
 297         "    </tr>\n",
 298         "    <tr>\n",
 299         "      <th>salesman</th>\n",
 300         "      <td>18</td>\n",
 301         "      <td>66</td>\n",
 302         "    </tr>\n",
 303         "    <tr>\n",
 304         "      <th>scientist</th>\n",
 305         "      <td>23</td>\n",
 306         "      <td>55</td>\n",
 307         "    </tr>\n",
 308         "    <tr>\n",
 309         "      <th>student</th>\n",
 310         "      <td>7</td>\n",
 311         "      <td>42</td>\n",
 312         "    </tr>\n",
 313         "    <tr>\n",
 314         "      <th>technician</th>\n",
 315         "      <td>21</td>\n",
 316         "      <td>55</td>\n",
 317         "    </tr>\n",
 318         "    <tr>\n",
 319         "      <th>writer</th>\n",
 320         "      <td>18</td>\n",
 321         "      <td>60</td>\n",
 322         "    </tr>\n",
 323         "  </tbody>\n",
 324         "</table>\n",
 325         "</div>"
 326        ],
 327        "text/plain": [
 328         "               Min Age  Max Age\n",
 329         "occupation                     \n",
 330         "administrator       21       70\n",
 331         "artist              19       48\n",
 332         "doctor              28       64\n",
 333         "educator            23       63\n",
 334         "engineer            22       70\n",
 335         "entertainment       15       50\n",
 336         "executive           22       69\n",
 337         "healthcare          22       62\n",
 338         "homemaker           20       50\n",
 339         "lawyer              21       53\n",
 340         "librarian           23       69\n",
 341         "marketing           24       55\n",
 342         "none                11       55\n",
 343         "other               13       64\n",
 344         "programmer          20       63\n",
 345         "retired             51       73\n",
 346         "salesman            18       66\n",
 347         "scientist           23       55\n",
 348         "student              7       42\n",
 349         "technician          21       55\n",
 350         "writer              18       60"
 351        ]
 352       },
 353       "execution_count": 8,
 354       "metadata": {},
 355       "output_type": "execute_result"
 356      }
 357     ],
 358     "source": [
 359      "df=pd.concat([min_age,max_age], axis=1)\n",
 360      "df"
 361     ]
 362    },
 363    {
 364     "cell_type": "code",
 365     "execution_count": 9,
 366     "id": "040b2a87",
 367     "metadata": {},
 368     "outputs": [],
 369     "source": [
 370      "df.to_csv('answer2.csv')"
 371     ]
 372    },
 373    {
 374     "cell_type": "code",
 375     "execution_count": null,
 376     "id": "25520200",
 377     "metadata": {},
 378     "outputs": [],
 379     "source": []
 380    }
 381   ],
 382   "metadata": {
 383    "kernelspec": {
 384     "display_name": "Python 3",
 385     "language": "python",
 386     "name": "python3"
 387    },
 388    "language_info": {
 389     "codemirror_mode": {
 390      "name": "ipython",
 391      "version": 3
 392     },
 393     "file_extension": ".py",
 394     "mimetype": "text/x-python",
 395     "name": "python",
 396     "nbconvert_exporter": "python",
 397     "pygments_lexer": "ipython3",
 398     "version": "3.8.8"
 399    }
 400   },
 401   "nbformat": 4,
 402   "nbformat_minor": 5
 403  }
 404