|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Pandas Essentials" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 3, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [ |
| 15 | + { |
| 16 | + "name": "stdout", |
| 17 | + "output_type": "stream", |
| 18 | + "text": [ |
| 19 | + " FlightID Airline Destination Duration Delay\n", |
| 20 | + "0 1 American Airline Sharjah 330 18\n", |
| 21 | + "1 2 Tata Airline Lahore 320 17\n", |
| 22 | + "2 3 PIA Washington 297 93\n", |
| 23 | + "3 4 Japan Airways Alaska 199 84\n", |
| 24 | + "4 5 Japan Airways Madina 146 2\n", |
| 25 | + " Duration Delay\n", |
| 26 | + "Airline \n", |
| 27 | + "American Airline 265.166667 50.777778\n", |
| 28 | + "Emirates 226.500000 61.700000\n", |
| 29 | + "Japan Airways 228.526316 55.736842\n", |
| 30 | + "PIA 227.125000 64.312500\n", |
| 31 | + "Qatar Airways 170.000000 65.875000\n", |
| 32 | + "Saudi Airline 213.500000 45.500000\n", |
| 33 | + "Tata Airline 215.444444 68.555556\n" |
| 34 | + ] |
| 35 | + } |
| 36 | + ], |
| 37 | + "source": [ |
| 38 | + "import pandas as pd\n", |
| 39 | + "import numpy as np\n", |
| 40 | + "\n", |
| 41 | + "# Create a random flights data CSV file\n", |
| 42 | + "np.random.seed(0)\n", |
| 43 | + "num_records = 100\n", |
| 44 | + "\n", |
| 45 | + "AIRLINES = ['PIA', 'Qatar Airways', 'Emirates',\"Japan Airways\",\"American Airline\",\"Tata Airline\", \"Saudi Airline\"]\n", |
| 46 | + "\n", |
| 47 | + "CITIES = [\n", |
| 48 | + " \"Dubai\",\n", |
| 49 | + " \"Delhi\",\n", |
| 50 | + " \"Karachi\",\n", |
| 51 | + " \"Riyad\",\n", |
| 52 | + " \"Makkah\",\n", |
| 53 | + " \"Madina\",\n", |
| 54 | + " \"Kuwait\",\n", |
| 55 | + " \"Lahore\",\n", |
| 56 | + " \"Colombo\",\n", |
| 57 | + " \"Dhaka\",\n", |
| 58 | + " \"Sharjah\",\n", |
| 59 | + " \"Mumbai\",\n", |
| 60 | + " \"Auckland\",\n", |
| 61 | + " \"Alaska\",\n", |
| 62 | + " \"San Francisco\",\n", |
| 63 | + " \"Washington\",\n", |
| 64 | + "]\n", |
| 65 | + "\n", |
| 66 | + "flights_data = {\n", |
| 67 | + " 'FlightID': np.arange(1, num_records + 1),\n", |
| 68 | + " 'Airline': np.random.choice(AIRLINES, num_records),\n", |
| 69 | + " 'Destination': np.random.choice(CITIES, num_records),\n", |
| 70 | + " 'Duration': np.random.randint(60, 360, num_records), # Duration in minutes\n", |
| 71 | + " 'Delay': np.random.randint(0, 120, num_records) # Delay in minutes\n", |
| 72 | + "}\n", |
| 73 | + "\n", |
| 74 | + "flights_df = pd.DataFrame(flights_data)\n", |
| 75 | + "flights_df.to_csv('random_flights_data.csv', index=False)\n", |
| 76 | + "\n", |
| 77 | + "# Read the CSV file using pandas\n", |
| 78 | + "flights_df = pd.read_csv('random_flights_data.csv')\n", |
| 79 | + "\n", |
| 80 | + "# Display the first few rows of the dataframe\n", |
| 81 | + "print(flights_df.head())\n", |
| 82 | + "\n", |
| 83 | + "# Perform operations on the data\n", |
| 84 | + "# Example: Calculate the average duration and delay for each airline\n", |
| 85 | + "average_stats = flights_df.groupby('Airline')[['Duration', 'Delay']].mean()\n", |
| 86 | + "print(average_stats)" |
| 87 | + ] |
| 88 | + } |
| 89 | + ], |
| 90 | + "metadata": { |
| 91 | + "kernelspec": { |
| 92 | + "display_name": "venv", |
| 93 | + "language": "python", |
| 94 | + "name": "python3" |
| 95 | + }, |
| 96 | + "language_info": { |
| 97 | + "codemirror_mode": { |
| 98 | + "name": "ipython", |
| 99 | + "version": 3 |
| 100 | + }, |
| 101 | + "file_extension": ".py", |
| 102 | + "mimetype": "text/x-python", |
| 103 | + "name": "python", |
| 104 | + "nbconvert_exporter": "python", |
| 105 | + "pygments_lexer": "ipython3", |
| 106 | + "version": "3.12.6" |
| 107 | + } |
| 108 | + }, |
| 109 | + "nbformat": 4, |
| 110 | + "nbformat_minor": 2 |
| 111 | +} |
0 commit comments