4
4
"metadata" : {
5
5
"colab" : {
6
6
"provenance" : [],
7
- "authorship_tag" : " ABX9TyP45TVE/kj6jRubvwxR8kn1 " ,
7
+ "authorship_tag" : " ABX9TyO4N8M+ba81EU5bXKbQdO/W " ,
8
8
"include_colab_link" : true
9
9
},
10
10
"kernelspec" : {
67
67
"metadata" : {
68
68
"id" : " 5jgqcBUNkPjV"
69
69
},
70
- "execution_count" : null ,
70
+ "execution_count" : 1 ,
71
71
"outputs" : []
72
72
},
73
+ {
74
+ "cell_type" : " code" ,
75
+ "source" : [
76
+ " import pandas as pd\n " ,
77
+ " # Create a DataFrame\n " ,
78
+ " df = pd.DataFrame(data)\n " ,
79
+ " \n " ,
80
+ " # Display the first few rows of the DataFrame\n " ,
81
+ " print(\" Initial DataFrame:\" )\n " ,
82
+ " print(df.head(), \"\\ n\" )\n " ,
83
+ " \n " ,
84
+ " # 1. Create a new column 'B_D_Ratio' as the ratio of column B to D\n " ,
85
+ " df['B_D_Ratio'] = df['B'] / df['D']\n " ,
86
+ " print(\" DataFrame with 'B_D_Ratio':\" )\n " ,
87
+ " print(df[['A', 'B', 'D', 'B_D_Ratio']].head(), \"\\ n\" )\n " ,
88
+ " \n " ,
89
+ " # 2. Filter rows where column B is greater than the median value of B\n " ,
90
+ " median_B = df['B'].median()\n " ,
91
+ " df_filtered = df[df['B'] > median_B]\n " ,
92
+ " print(f\" Filtered DataFrame (B > {median_B}):\" )\n " ,
93
+ " print(df_filtered[['A', 'B']].reset_index(drop=True), \"\\ n\" )\n " ,
94
+ " \n " ,
95
+ " # 3. Create a new column 'Country_initial' with the first letter of the country name (column A)\n " ,
96
+ " df['Country_initial'] = df['A'].str[0]\n " ,
97
+ " print(\" DataFrame with 'Country_initial':\" )\n " ,
98
+ " print(df[['A', 'Country_initial']].head(), \"\\ n\" )\n " ,
99
+ " \n " ,
100
+ " # 4. Group by the 'Country_initial' and compute the mean of columns B and D\n " ,
101
+ " grouped = df.groupby('Country_initial')[['B', 'D']].mean().reset_index()\n " ,
102
+ " print(\" Grouped DataFrame (mean of B and D by Country_initial):\" )\n " ,
103
+ " print(grouped, \"\\ n\" )\n " ,
104
+ " \n " ,
105
+ " # 5. Sort the DataFrame by column C in descending order\n " ,
106
+ " df_sorted = df.sort_values(by='C', ascending=False)\n " ,
107
+ " print(\" DataFrame sorted by column C (descending):\" )\n " ,
108
+ " print(df_sorted[['A', 'C']].head())"
109
+ ],
110
+ "metadata" : {
111
+ "colab" : {
112
+ "base_uri" : " https://localhost:8080/"
113
+ },
114
+ "id" : " CJe0PUuRSKge" ,
115
+ "outputId" : " 694a12b8-1d98-4423-9cd1-cd4f2ef440ed"
116
+ },
117
+ "execution_count" : 3 ,
118
+ "outputs" : [
119
+ {
120
+ "output_type" : " stream" ,
121
+ "name" : " stdout" ,
122
+ "text" : [
123
+ " Initial DataFrame:\n " ,
124
+ " A B C D E F\n " ,
125
+ " 0 Argentina 960.94 11.23 242619 78.88 45.38\n " ,
126
+ " 1 Australia 42.39 6.44 1971859 77.25 75.87\n " ,
127
+ " 2 Austria 416.17 8.16 9058867 77.82 69.73\n " ,
128
+ " 3 Belgium 338.70 28.11 4526583 69.97 40.81\n " ,
129
+ " 4 Brazil 1106.02 19.51 5757197 76.79 45.13 \n " ,
130
+ " \n " ,
131
+ " DataFrame with 'B_D_Ratio':\n " ,
132
+ " A B D B_D_Ratio\n " ,
133
+ " 0 Argentina 960.94 242619 0.003961\n " ,
134
+ " 1 Australia 42.39 1971859 0.000021\n " ,
135
+ " 2 Austria 416.17 9058867 0.000046\n " ,
136
+ " 3 Belgium 338.70 4526583 0.000075\n " ,
137
+ " 4 Brazil 1106.02 5757197 0.000192 \n " ,
138
+ " \n " ,
139
+ " Filtered DataFrame (B > 698.14):\n " ,
140
+ " A B\n " ,
141
+ " 0 Argentina 960.94\n " ,
142
+ " 1 Brazil 1106.02\n " ,
143
+ " 2 Canada 1016.67\n " ,
144
+ " 3 China 1338.81\n " ,
145
+ " 4 France 760.51\n " ,
146
+ " 5 India 976.58\n " ,
147
+ " 6 Indonesia 819.69\n " ,
148
+ " 7 Italy 885.95\n " ,
149
+ " 8 Japan 1215.10\n " ,
150
+ " 9 Mexico 1209.70\n " ,
151
+ " 10 Netherlands 1048.72\n " ,
152
+ " 11 Pakistan 1436.03\n " ,
153
+ " 12 Spain 1272.00\n " ,
154
+ " 13 United States 907.57\n " ,
155
+ " 14 Bangladesh 1211.66\n " ,
156
+ " 15 Vietnam 1095.95\n " ,
157
+ " 16 Turkey 806.66\n " ,
158
+ " 17 South Africa 1459.81\n " ,
159
+ " 18 Ukraine 830.30\n " ,
160
+ " 19 Poland 1244.96\n " ,
161
+ " 20 Saudi Arabia 929.69\n " ,
162
+ " 21 Argentina 1293.25\n " ,
163
+ " 22 Sweden 868.14\n " ,
164
+ " 23 Switzerland 1058.33\n " ,
165
+ " 24 Peru 955.35 \n " ,
166
+ " \n " ,
167
+ " DataFrame with 'Country_initial':\n " ,
168
+ " A Country_initial\n " ,
169
+ " 0 Argentina A\n " ,
170
+ " 1 Australia A\n " ,
171
+ " 2 Austria A\n " ,
172
+ " 3 Belgium B\n " ,
173
+ " 4 Brazil B \n " ,
174
+ " \n " ,
175
+ " Grouped DataFrame (mean of B and D by Country_initial):\n " ,
176
+ " Country_initial B D\n " ,
177
+ " 0 A 678.187500 3.121134e+06\n " ,
178
+ " 1 B 682.472500 4.424265e+06\n " ,
179
+ " 2 C 727.755000 5.347219e+06\n " ,
180
+ " 3 D 635.770000 2.703446e+06\n " ,
181
+ " 4 E 49.550000 7.662220e+06\n " ,
182
+ " 5 F 546.185000 2.286690e+06\n " ,
183
+ " 6 G 173.465000 5.747861e+06\n " ,
184
+ " 7 H 156.000000 8.902897e+06\n " ,
185
+ " 8 I 754.195000 6.006751e+06\n " ,
186
+ " 9 J 1215.100000 3.387174e+06\n " ,
187
+ " 10 M 449.570000 3.919273e+06\n " ,
188
+ " 11 N 587.562500 7.476341e+06\n " ,
189
+ " 12 P 1036.137500 2.659610e+06\n " ,
190
+ " 13 R 143.660000 8.247443e+06\n " ,
191
+ " 14 S 870.082857 4.320352e+06\n " ,
192
+ " 15 T 605.070000 6.621555e+06\n " ,
193
+ " 16 U 694.526667 5.178348e+06\n " ,
194
+ " 17 V 1095.950000 4.067343e+06 \n " ,
195
+ " \n " ,
196
+ " DataFrame sorted by column C (descending):\n " ,
197
+ " A C\n " ,
198
+ " 36 Poland 29.93\n " ,
199
+ " 46 Hong Kong 29.88\n " ,
200
+ " 10 Finland 29.69\n " ,
201
+ " 48 Peru 29.14\n " ,
202
+ " 21 Netherlands 28.30\n "
203
+ ]
204
+ }
205
+ ]
206
+ },
73
207
{
74
208
"cell_type" : " code" ,
75
209
"source" : [
1344
1478
]
1345
1479
}
1346
1480
]
1481
+ },
1482
+ {
1483
+ "cell_type" : " code" ,
1484
+ "source" : [],
1485
+ "metadata" : {
1486
+ "id" : " 7sb6BOq9NYAq"
1487
+ },
1488
+ "execution_count" : null ,
1489
+ "outputs" : []
1490
+ },
1491
+ {
1492
+ "cell_type" : " code" ,
1493
+ "source" : [
1494
+ " import requests\n " ,
1495
+ " \n " ,
1496
+ " def fetch_pokemon_names():\n " ,
1497
+ " \"\"\" Generator function to yield Pokémon names from the API\"\"\"\n " ,
1498
+ " url = \" https://pokeapi.co/api/v2/pokemon?limit=2000\"\n " ,
1499
+ " response = requests.get(url)\n " ,
1500
+ " \n " ,
1501
+ " if response.status_code == 200:\n " ,
1502
+ " data = response.json()\n " ,
1503
+ " for pokemon in data['results']: # Iterate over results\n " ,
1504
+ " yield pokemon['name'] # Yield one name at a time\n " ,
1505
+ " else:\n " ,
1506
+ " yield \" Failed to fetch data from PokeAPI\"\n " ,
1507
+ " \n " ,
1508
+ " # Using the generator\n " ,
1509
+ " pokemon_generator = fetch_pokemon_names()\n " ,
1510
+ " \n " ,
1511
+ " # Example: Print first 10 Pokémon names\n " ,
1512
+ " print(\" Available Pokémon names:\" )\n " ,
1513
+ " for i, name in enumerate(pokemon_generator):\n " ,
1514
+ " if i >= 10: # Stop after 10 names for demonstration\n " ,
1515
+ " break\n " ,
1516
+ " print(name)\n "
1517
+ ],
1518
+ "metadata" : {
1519
+ "colab" : {
1520
+ "base_uri" : " https://localhost:8080/"
1521
+ },
1522
+ "id" : " poFjyF2vMHJy" ,
1523
+ "outputId" : " de1b1166-c883-4948-f2c5-20b0d2c89848"
1524
+ },
1525
+ "execution_count" : null ,
1526
+ "outputs" : [
1527
+ {
1528
+ "output_type" : " stream" ,
1529
+ "name" : " stdout" ,
1530
+ "text" : [
1531
+ " Available Pokémon names:\n " ,
1532
+ " bulbasaur\n " ,
1533
+ " ivysaur\n " ,
1534
+ " venusaur\n " ,
1535
+ " charmander\n " ,
1536
+ " charmeleon\n " ,
1537
+ " charizard\n " ,
1538
+ " squirtle\n " ,
1539
+ " wartortle\n " ,
1540
+ " blastoise\n " ,
1541
+ " caterpie\n "
1542
+ ]
1543
+ }
1544
+ ]
1545
+ },
1546
+ {
1547
+ "cell_type" : " code" ,
1548
+ "source" : [
1549
+ " import requests\n " ,
1550
+ " \n " ,
1551
+ " def fetch_pokemon_batches(batch_size=10):\n " ,
1552
+ " \"\"\" Generator function to yield Pokémon names in batches\"\"\"\n " ,
1553
+ " url = \" https://pokeapi.co/api/v2/pokemon?limit=2000\"\n " ,
1554
+ " response = requests.get(url)\n " ,
1555
+ " \n " ,
1556
+ " if response.status_code == 200:\n " ,
1557
+ " data = response.json()\n " ,
1558
+ " pokemon_list = [pokemon['name'] for pokemon in data['results']]\n " ,
1559
+ " \n " ,
1560
+ " # Yield batches of batch_size\n " ,
1561
+ " for i in range(0, len(pokemon_list), batch_size):\n " ,
1562
+ " yield pokemon_list[i:i + batch_size] # Yield a batch of 10 Pokémon names\n " ,
1563
+ " else:\n " ,
1564
+ " yield [\" Failed to fetch data from PokeAPI\" ]\n " ,
1565
+ " \n " ,
1566
+ " # Using the generator to fetch Pokémon in batches of 10\n " ,
1567
+ " pokemon_batch_generator = fetch_pokemon_batches(batch_size=10)\n " ,
1568
+ " \n " ,
1569
+ " # Example: Print the first 5 batches\n " ,
1570
+ " print(\" Available Pokémon names in batches of 10:\" )\n " ,
1571
+ " for i, batch in enumerate(pokemon_batch_generator):\n " ,
1572
+ " if i >= 5: # Stop after 5 batches for demonstration\n " ,
1573
+ " break\n " ,
1574
+ " print(f\" Batch {i + 1}: {batch}\" )\n "
1575
+ ],
1576
+ "metadata" : {
1577
+ "id" : " 0z4s0HsBLv-J" ,
1578
+ "outputId" : " 8fea6983-4f86-4d9d-d579-8f4dbf12a3f3" ,
1579
+ "colab" : {
1580
+ "base_uri" : " https://localhost:8080/"
1581
+ }
1582
+ },
1583
+ "execution_count" : null ,
1584
+ "outputs" : [
1585
+ {
1586
+ "output_type" : " stream" ,
1587
+ "name" : " stdout" ,
1588
+ "text" : [
1589
+ " Available Pokémon names in batches of 10:\n " ,
1590
+ " Batch 1: ['bulbasaur', 'ivysaur', 'venusaur', 'charmander', 'charmeleon', 'charizard', 'squirtle', 'wartortle', 'blastoise', 'caterpie']\n " ,
1591
+ " Batch 2: ['metapod', 'butterfree', 'weedle', 'kakuna', 'beedrill', 'pidgey', 'pidgeotto', 'pidgeot', 'rattata', 'raticate']\n " ,
1592
+ " Batch 3: ['spearow', 'fearow', 'ekans', 'arbok', 'pikachu', 'raichu', 'sandshrew', 'sandslash', 'nidoran-f', 'nidorina']\n " ,
1593
+ " Batch 4: ['nidoqueen', 'nidoran-m', 'nidorino', 'nidoking', 'clefairy', 'clefable', 'vulpix', 'ninetales', 'jigglypuff', 'wigglytuff']\n " ,
1594
+ " Batch 5: ['zubat', 'golbat', 'oddish', 'gloom', 'vileplume', 'paras', 'parasect', 'venonat', 'venomoth', 'diglett']\n "
1595
+ ]
1596
+ }
1597
+ ]
1598
+ },
1599
+ {
1600
+ "cell_type" : " code" ,
1601
+ "source" : [
1602
+ " import requests\n " ,
1603
+ " \n " ,
1604
+ " api_base=a\n " ,
1605
+ " api_key=b\n " ,
1606
+ " client_id=c\n " ,
1607
+ " \n " ,
1608
+ " def get_domain():\n " ,
1609
+ " url=f\" {api_base}/domains/{client_id}?api_key={api_key}\"\n " ,
1610
+ " response = requests.get(url)\n " ,
1611
+ " response.raise_for_status()\n " ,
1612
+ " return response.json()\n " ,
1613
+ " \n " ,
1614
+ " def get_"
1615
+ ],
1616
+ "metadata" : {
1617
+ "id" : " o_WT1MFhHz5M"
1618
+ },
1619
+ "execution_count" : null ,
1620
+ "outputs" : []
1347
1621
}
1348
1622
]
1349
1623
}
0 commit comments