Skip to content

Commit efd92f7

Browse files
committed
Refactored the code
1 parent 69ab9d4 commit efd92f7

File tree

1 file changed

+227
-35
lines changed

1 file changed

+227
-35
lines changed

9-Pandas/3-pandas.ipynb

+227-35
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,15 @@
99
},
1010
{
1111
"cell_type": "code",
12-
"execution_count": 3,
12+
"execution_count": 25,
1313
"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-
],
14+
"outputs": [],
3715
"source": [
3816
"import pandas as pd\n",
3917
"import numpy as np\n",
4018
"\n",
4119
"# Create a random flights data CSV file\n",
42-
"np.random.seed(0)\n",
20+
"# np.random.seed(0)\n",
4321
"num_records = 100\n",
4422
"\n",
4523
"AIRLINES = [\n",
@@ -56,9 +34,9 @@
5634
" \"Dubai\",\n",
5735
" \"Delhi\",\n",
5836
" \"Karachi\",\n",
59-
" \"Riyad\",\n",
60-
" \"Makkah\",\n",
61-
" \"Madina\",\n",
37+
" \"Riyadh\",\n",
38+
" \"Mecca\",\n",
39+
" \"Medina\",\n",
6240
" \"Kuwait\",\n",
6341
" \"Lahore\",\n",
6442
" \"Colombo\",\n",
@@ -69,8 +47,15 @@
6947
" \"Alaska\",\n",
7048
" \"San Francisco\",\n",
7149
" \"Washington\",\n",
72-
"]\n",
73-
"\n",
50+
"]\n"
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": 26,
56+
"metadata": {},
57+
"outputs": [],
58+
"source": [
7459
"flights_data = {\n",
7560
" \"FlightID\": np.arange(1, num_records + 1),\n",
7661
" \"Airline\": np.random.choice(AIRLINES, num_records),\n",
@@ -80,18 +65,225 @@
8065
"}\n",
8166
"\n",
8267
"flights_df = pd.DataFrame(flights_data)\n",
83-
"flights_df.to_csv(\"random_flights_data.csv\", index=False)\n",
84-
"\n",
68+
"flights_df.to_csv(\"random_flights_data.csv\", index=False)\n"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 27,
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"data": {
78+
"text/html": [
79+
"<div>\n",
80+
"<style scoped>\n",
81+
" .dataframe tbody tr th:only-of-type {\n",
82+
" vertical-align: middle;\n",
83+
" }\n",
84+
"\n",
85+
" .dataframe tbody tr th {\n",
86+
" vertical-align: top;\n",
87+
" }\n",
88+
"\n",
89+
" .dataframe thead th {\n",
90+
" text-align: right;\n",
91+
" }\n",
92+
"</style>\n",
93+
"<table border=\"1\" class=\"dataframe\">\n",
94+
" <thead>\n",
95+
" <tr style=\"text-align: right;\">\n",
96+
" <th></th>\n",
97+
" <th>FlightID</th>\n",
98+
" <th>Airline</th>\n",
99+
" <th>Destination</th>\n",
100+
" <th>Duration</th>\n",
101+
" <th>Delay</th>\n",
102+
" </tr>\n",
103+
" </thead>\n",
104+
" <tbody>\n",
105+
" <tr>\n",
106+
" <th>0</th>\n",
107+
" <td>1</td>\n",
108+
" <td>Tata Airline</td>\n",
109+
" <td>Mecca</td>\n",
110+
" <td>342</td>\n",
111+
" <td>114</td>\n",
112+
" </tr>\n",
113+
" <tr>\n",
114+
" <th>1</th>\n",
115+
" <td>2</td>\n",
116+
" <td>Emirates</td>\n",
117+
" <td>Dubai</td>\n",
118+
" <td>266</td>\n",
119+
" <td>57</td>\n",
120+
" </tr>\n",
121+
" <tr>\n",
122+
" <th>2</th>\n",
123+
" <td>3</td>\n",
124+
" <td>Emirates</td>\n",
125+
" <td>Dubai</td>\n",
126+
" <td>92</td>\n",
127+
" <td>91</td>\n",
128+
" </tr>\n",
129+
" <tr>\n",
130+
" <th>3</th>\n",
131+
" <td>4</td>\n",
132+
" <td>American Airline</td>\n",
133+
" <td>Washington</td>\n",
134+
" <td>175</td>\n",
135+
" <td>89</td>\n",
136+
" </tr>\n",
137+
" <tr>\n",
138+
" <th>4</th>\n",
139+
" <td>5</td>\n",
140+
" <td>Saudi Airline</td>\n",
141+
" <td>Sharjah</td>\n",
142+
" <td>238</td>\n",
143+
" <td>3</td>\n",
144+
" </tr>\n",
145+
" </tbody>\n",
146+
"</table>\n",
147+
"</div>"
148+
],
149+
"text/plain": [
150+
" FlightID Airline Destination Duration Delay\n",
151+
"0 1 Tata Airline Mecca 342 114\n",
152+
"1 2 Emirates Dubai 266 57\n",
153+
"2 3 Emirates Dubai 92 91\n",
154+
"3 4 American Airline Washington 175 89\n",
155+
"4 5 Saudi Airline Sharjah 238 3"
156+
]
157+
},
158+
"metadata": {},
159+
"output_type": "display_data"
160+
}
161+
],
162+
"source": [
85163
"# Read the CSV file using pandas\n",
86164
"flights_df = pd.read_csv(\"random_flights_data.csv\")\n",
87165
"\n",
88166
"# Display the first few rows of the dataframe\n",
89-
"print(flights_df.head())\n",
90-
"\n",
167+
"display(flights_df.head())\n"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 28,
173+
"metadata": {},
174+
"outputs": [
175+
{
176+
"data": {
177+
"text/html": [
178+
"<div>\n",
179+
"<style scoped>\n",
180+
" .dataframe tbody tr th:only-of-type {\n",
181+
" vertical-align: middle;\n",
182+
" }\n",
183+
"\n",
184+
" .dataframe tbody tr th {\n",
185+
" vertical-align: top;\n",
186+
" }\n",
187+
"\n",
188+
" .dataframe thead th {\n",
189+
" text-align: right;\n",
190+
" }\n",
191+
"</style>\n",
192+
"<table border=\"1\" class=\"dataframe\">\n",
193+
" <thead>\n",
194+
" <tr style=\"text-align: right;\">\n",
195+
" <th></th>\n",
196+
" <th>Duration</th>\n",
197+
" <th>Delay</th>\n",
198+
" </tr>\n",
199+
" <tr>\n",
200+
" <th>Airline</th>\n",
201+
" <th></th>\n",
202+
" <th></th>\n",
203+
" </tr>\n",
204+
" </thead>\n",
205+
" <tbody>\n",
206+
" <tr>\n",
207+
" <th>American Airline</th>\n",
208+
" <td>181.600000</td>\n",
209+
" <td>71.600000</td>\n",
210+
" </tr>\n",
211+
" <tr>\n",
212+
" <th>Emirates</th>\n",
213+
" <td>219.769231</td>\n",
214+
" <td>64.538462</td>\n",
215+
" </tr>\n",
216+
" <tr>\n",
217+
" <th>Japan Airways</th>\n",
218+
" <td>181.230769</td>\n",
219+
" <td>73.307692</td>\n",
220+
" </tr>\n",
221+
" <tr>\n",
222+
" <th>PIA</th>\n",
223+
" <td>226.357143</td>\n",
224+
" <td>47.285714</td>\n",
225+
" </tr>\n",
226+
" <tr>\n",
227+
" <th>Qatar Airways</th>\n",
228+
" <td>249.684211</td>\n",
229+
" <td>65.157895</td>\n",
230+
" </tr>\n",
231+
" <tr>\n",
232+
" <th>Saudi Airline</th>\n",
233+
" <td>214.928571</td>\n",
234+
" <td>64.928571</td>\n",
235+
" </tr>\n",
236+
" <tr>\n",
237+
" <th>Tata Airline</th>\n",
238+
" <td>196.416667</td>\n",
239+
" <td>67.833333</td>\n",
240+
" </tr>\n",
241+
" </tbody>\n",
242+
"</table>\n",
243+
"</div>"
244+
],
245+
"text/plain": [
246+
" Duration Delay\n",
247+
"Airline \n",
248+
"American Airline 181.600000 71.600000\n",
249+
"Emirates 219.769231 64.538462\n",
250+
"Japan Airways 181.230769 73.307692\n",
251+
"PIA 226.357143 47.285714\n",
252+
"Qatar Airways 249.684211 65.157895\n",
253+
"Saudi Airline 214.928571 64.928571\n",
254+
"Tata Airline 196.416667 67.833333"
255+
]
256+
},
257+
"metadata": {},
258+
"output_type": "display_data"
259+
},
260+
{
261+
"data": {
262+
"text/plain": [
263+
"'Max Delay: 73.3076923076923 minutes'"
264+
]
265+
},
266+
"metadata": {},
267+
"output_type": "display_data"
268+
},
269+
{
270+
"data": {
271+
"text/plain": [
272+
"'Max Duration: 249.68421052631578 minutes'"
273+
]
274+
},
275+
"metadata": {},
276+
"output_type": "display_data"
277+
}
278+
],
279+
"source": [
91280
"# Perform operations on the data\n",
92281
"# Example: Calculate the average duration and delay for each airline\n",
93282
"average_stats = flights_df.groupby(\"Airline\")[[\"Duration\", \"Delay\"]].mean()\n",
94-
"print(average_stats)"
283+
"display(average_stats)\n",
284+
"\n",
285+
"display(f\"Max Delay: {average_stats['Delay'].max()} minutes\")\n",
286+
"display(f\"Max Duration: {average_stats['Duration'].max()} minutes\")"
95287
]
96288
}
97289
],

0 commit comments

Comments
 (0)