|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import pandas as pd\n", |
| 10 | + "import numpy as np" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "1. Pandas in the open source python library data analysis. Its a powerful library. \n", |
| 18 | + "2. Python has great for manipulating data but pandas make it more powerful. Pandas is very fast and quick because it is based on spilled on the top of numpy. It has two data structures: series and data frames. \n", |
| 19 | + "3. Data frameswill be heavily used. \n", |
| 20 | + "4. Series can be thought of as one dimensional array object. \n", |
| 21 | + "3. It is one dimensional array or list or column in a table but it can have labels.\n", |
| 22 | + "4. Labelled arrays or labelled columns or vectors are sort of one dimensional indexed array. \n", |
| 23 | + "5. In addition to index, each element can have a name. so each element can be accessed either by index or name\n", |
| 24 | + "6. Data frame is powerful data structure and comes in pandas package. \n", |
| 25 | + "7.Data frames are 2 dimensional labelled data structure with columns of different types. They are 2 dimensional in a grid like format (like 2 dimensional array or Database tables or Excel sheet ).\n", |
| 26 | + "8. They have labelled columns and rows and data can of different types.\n", |
| 27 | + "Data frames consists of components: 1. Data itself, index and the column\n" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 3, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "name": "stdout", |
| 37 | + "output_type": "stream", |
| 38 | + "text": [ |
| 39 | + "[['ihsan' '20' 'lahore']\n", |
| 40 | + " ['femala' '21' 'shabe qadar']\n", |
| 41 | + " ['engin' '21' 'swabi']]\n" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
| 45 | + "source": [ |
| 46 | + "import numpy as np\n", |
| 47 | + "a=np.array([('ihsan',20,'lahore'),('femala',21,'shabe qadar'),('engin',21,'swabi')])\n", |
| 48 | + "print(a)" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 4, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "import pandas as pd" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": 5, |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [ |
| 65 | + { |
| 66 | + "data": { |
| 67 | + "text/plain": [ |
| 68 | + "0 atif\n", |
| 69 | + "1 80\n", |
| 70 | + "2 Pehwar\n", |
| 71 | + "dtype: object" |
| 72 | + ] |
| 73 | + }, |
| 74 | + "execution_count": 5, |
| 75 | + "metadata": {}, |
| 76 | + "output_type": "execute_result" |
| 77 | + } |
| 78 | + ], |
| 79 | + "source": [ |
| 80 | + "series=pd.Series(['atif',80,'Pehwar'])\n", |
| 81 | + "series" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": 6, |
| 87 | + "metadata": {}, |
| 88 | + "outputs": [ |
| 89 | + { |
| 90 | + "data": { |
| 91 | + "text/plain": [ |
| 92 | + "0 ali\n", |
| 93 | + "1 37\n", |
| 94 | + "2 Peshawar\n", |
| 95 | + "3 5.5\n", |
| 96 | + "dtype: object" |
| 97 | + ] |
| 98 | + }, |
| 99 | + "execution_count": 6, |
| 100 | + "metadata": {}, |
| 101 | + "output_type": "execute_result" |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "series1=pd.Series(['ali',37,'Peshawar',5.5]) # create a series by passing a list of different elements\n", |
| 106 | + "series1" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 7, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "data": { |
| 116 | + "text/plain": [ |
| 117 | + "0 ali\n", |
| 118 | + "1 37\n", |
| 119 | + "2 Peshawar\n", |
| 120 | + "3 5.5\n", |
| 121 | + "dtype: object" |
| 122 | + ] |
| 123 | + }, |
| 124 | + "execution_count": 7, |
| 125 | + "metadata": {}, |
| 126 | + "output_type": "execute_result" |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "series1 # series is one dimensional object array" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 8, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [ |
| 138 | + { |
| 139 | + "data": { |
| 140 | + "text/plain": [ |
| 141 | + "pandas.core.series.Series" |
| 142 | + ] |
| 143 | + }, |
| 144 | + "execution_count": 8, |
| 145 | + "metadata": {}, |
| 146 | + "output_type": "execute_result" |
| 147 | + } |
| 148 | + ], |
| 149 | + "source": [ |
| 150 | + "type(series1)" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": 11, |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [ |
| 158 | + { |
| 159 | + "data": { |
| 160 | + "text/plain": [ |
| 161 | + "name ali\n", |
| 162 | + "age 37\n", |
| 163 | + "city Peshawar\n", |
| 164 | + "hgt 5.5\n", |
| 165 | + "dtype: object" |
| 166 | + ] |
| 167 | + }, |
| 168 | + "execution_count": 11, |
| 169 | + "metadata": {}, |
| 170 | + "output_type": "execute_result" |
| 171 | + } |
| 172 | + ], |
| 173 | + "source": [ |
| 174 | + "ser1=pd.Series(['ali',37,'Peshawar',5.5] , index=['name','age','city','hgt']) #create a series by passing two list # list of values and list or array of indices \n", |
| 175 | + "ser1" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": 14, |
| 181 | + "metadata": { |
| 182 | + "scrolled": true |
| 183 | + }, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "data": { |
| 187 | + "text/plain": [ |
| 188 | + "'ali'" |
| 189 | + ] |
| 190 | + }, |
| 191 | + "execution_count": 14, |
| 192 | + "metadata": {}, |
| 193 | + "output_type": "execute_result" |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "#ser1['name']\n", |
| 198 | + "ser1.iloc[0]" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "code", |
| 203 | + "execution_count": 19, |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [ |
| 206 | + { |
| 207 | + "name": "stdout", |
| 208 | + "output_type": "stream", |
| 209 | + "text": [ |
| 210 | + "ali\n" |
| 211 | + ] |
| 212 | + } |
| 213 | + ], |
| 214 | + "source": [ |
| 215 | + "print(ser1[0])# elements of series object canbe accessed by numeric index or labels (provided by you as index)" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": 17, |
| 221 | + "metadata": {}, |
| 222 | + "outputs": [ |
| 223 | + { |
| 224 | + "data": { |
| 225 | + "text/plain": [ |
| 226 | + "'ali'" |
| 227 | + ] |
| 228 | + }, |
| 229 | + "execution_count": 17, |
| 230 | + "metadata": {}, |
| 231 | + "output_type": "execute_result" |
| 232 | + } |
| 233 | + ], |
| 234 | + "source": [ |
| 235 | + "ser1['name'] #ser1[1] #ser1['age'] or ser1[1]" |
| 236 | + ] |
| 237 | + }, |
| 238 | + { |
| 239 | + "cell_type": "code", |
| 240 | + "execution_count": 15, |
| 241 | + "metadata": {}, |
| 242 | + "outputs": [], |
| 243 | + "source": [ |
| 244 | + "my_series = pd.Series({'London':10,'Tripoli':100,'Cairo':10}) #create a series by passing a dictionary" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 16, |
| 250 | + "metadata": {}, |
| 251 | + "outputs": [ |
| 252 | + { |
| 253 | + "data": { |
| 254 | + "text/plain": [ |
| 255 | + "London 10\n", |
| 256 | + "Tripoli 100\n", |
| 257 | + "Cairo 10\n", |
| 258 | + "dtype: int64" |
| 259 | + ] |
| 260 | + }, |
| 261 | + "execution_count": 16, |
| 262 | + "metadata": {}, |
| 263 | + "output_type": "execute_result" |
| 264 | + } |
| 265 | + ], |
| 266 | + "source": [ |
| 267 | + "my_series # dictionary do not care about the order, we can acces the value by the key" |
| 268 | + ] |
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "code", |
| 272 | + "execution_count": 17, |
| 273 | + "metadata": {}, |
| 274 | + "outputs": [ |
| 275 | + { |
| 276 | + "data": { |
| 277 | + "text/plain": [ |
| 278 | + "100" |
| 279 | + ] |
| 280 | + }, |
| 281 | + "execution_count": 17, |
| 282 | + "metadata": {}, |
| 283 | + "output_type": "execute_result" |
| 284 | + } |
| 285 | + ], |
| 286 | + "source": [ |
| 287 | + "my_series['Tripoli']" |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "cell_type": "code", |
| 292 | + "execution_count": 19, |
| 293 | + "metadata": {}, |
| 294 | + "outputs": [ |
| 295 | + { |
| 296 | + "data": { |
| 297 | + "text/plain": [ |
| 298 | + "London 10\n", |
| 299 | + "Tripoli 100\n", |
| 300 | + "Cairo 10\n", |
| 301 | + "dtype: int64" |
| 302 | + ] |
| 303 | + }, |
| 304 | + "execution_count": 19, |
| 305 | + "metadata": {}, |
| 306 | + "output_type": "execute_result" |
| 307 | + } |
| 308 | + ], |
| 309 | + "source": [ |
| 310 | + "my_series[my_series > 1] # filtering the dictionary" |
| 311 | + ] |
| 312 | + }, |
| 313 | + { |
| 314 | + "cell_type": "code", |
| 315 | + "execution_count": 20, |
| 316 | + "metadata": {}, |
| 317 | + "outputs": [ |
| 318 | + { |
| 319 | + "data": { |
| 320 | + "text/plain": [ |
| 321 | + "London 10\n", |
| 322 | + "Cairo 10\n", |
| 323 | + "dtype: int64" |
| 324 | + ] |
| 325 | + }, |
| 326 | + "execution_count": 20, |
| 327 | + "metadata": {}, |
| 328 | + "output_type": "execute_result" |
| 329 | + } |
| 330 | + ], |
| 331 | + "source": [ |
| 332 | + "my_series[my_series == 10] # filtering the dictionary" |
| 333 | + ] |
| 334 | + }, |
| 335 | + { |
| 336 | + "cell_type": "code", |
| 337 | + "execution_count": 22, |
| 338 | + "metadata": {}, |
| 339 | + "outputs": [ |
| 340 | + { |
| 341 | + "data": { |
| 342 | + "text/plain": [ |
| 343 | + "Tripoli 100\n", |
| 344 | + "dtype: int64" |
| 345 | + ] |
| 346 | + }, |
| 347 | + "execution_count": 22, |
| 348 | + "metadata": {}, |
| 349 | + "output_type": "execute_result" |
| 350 | + } |
| 351 | + ], |
| 352 | + "source": [ |
| 353 | + "my_series[my_series == 100] # filtering the dictionary" |
| 354 | + ] |
| 355 | + }, |
| 356 | + { |
| 357 | + "cell_type": "code", |
| 358 | + "execution_count": null, |
| 359 | + "metadata": {}, |
| 360 | + "outputs": [], |
| 361 | + "source": [] |
| 362 | + } |
| 363 | + ], |
| 364 | + "metadata": { |
| 365 | + "anaconda-cloud": {}, |
| 366 | + "kernelspec": { |
| 367 | + "display_name": "Python 3 (ipykernel)", |
| 368 | + "language": "python", |
| 369 | + "name": "python3" |
| 370 | + }, |
| 371 | + "language_info": { |
| 372 | + "codemirror_mode": { |
| 373 | + "name": "ipython", |
| 374 | + "version": 3 |
| 375 | + }, |
| 376 | + "file_extension": ".py", |
| 377 | + "mimetype": "text/x-python", |
| 378 | + "name": "python", |
| 379 | + "nbconvert_exporter": "python", |
| 380 | + "pygments_lexer": "ipython3", |
| 381 | + "version": "3.12.2" |
| 382 | + } |
| 383 | + }, |
| 384 | + "nbformat": 4, |
| 385 | + "nbformat_minor": 4 |
| 386 | +} |
0 commit comments