2023-programowanie-w-pythonie/zajecia2/zad_3.ipynb

445 lines
22 KiB
Plaintext
Raw Normal View History

2023-11-18 10:24:03 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python: Drugie zadanie domowe"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Drugie zadanie domowe będzie polegać na opracowaniu danych zawartych w pliku `gapminder.csv`, który znajduje się w tym katalogu (proszę wykorzystać ten plik, a nie ten w katalogu labs04). Ten arkusz poprowadzi Cię krok po kroku po zadaniu domowym."
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 1** Wczytaj dane do zmiennej `data`, w taki sposób, żeby nazwa Państwa była kluczem."
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 2** Znajdź najbardziej i najmniej zaludnione państwa na świecie."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 3** W ilu państwach współczynnik `female_BMI` jest większy od `male_BMI`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 4**\n",
" 1. Zainstaluj bibliotekę `pycountry_convert` i zaimportuj ją.\n",
" 1. Dodaj do danych kolumnę `continent`, która będzie zawierać nazwę kontynentu, na którym jest położone dane państwo. Wykorzystaj bibliotekę `pycountry_convert`. *Uwaga*: trzeba najpierw uzystać kod państwa w fomacie ISO-2, następnie uzystkać kod kontynentu, a na końcu uzyskać nazwę kontynentu."
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Afghanistan Asia\n",
"Albania Europe\n",
"Algeria Africa\n",
"Angola Africa\n",
"Antigua and Barbuda North America\n",
"Argentina South America\n",
"Armenia Asia\n",
"Australia Oceania\n",
"Austria Europe\n",
"Azerbaijan Asia\n",
"Bahamas North America\n",
"Bahrain Asia\n",
"Bangladesh Asia\n",
"Barbados North America\n",
"Belarus Europe\n",
"Belgium Europe\n",
"Belize North America\n",
"Benin Africa\n",
"Bhutan Asia\n",
"Bolivia South America\n",
"Bosnia and Herzegovina Europe\n",
"Botswana Africa\n",
"Brazil South America\n",
"Brunei Asia\n",
"Bulgaria Europe\n",
"Burkina Faso Africa\n",
"Burundi Africa\n",
"Cambodia Asia\n",
"Cameroon Africa\n",
"Canada North America\n",
" ... \n",
"Spain Europe\n",
"Sri Lanka Asia\n",
"Sudan Africa\n",
"Suriname South America\n",
"Swaziland Africa\n",
"Sweden Europe\n",
"Switzerland Europe\n",
"Syria Asia\n",
"Tajikistan Asia\n",
"Tanzania Africa\n",
"Thailand Asia\n",
"Togo Africa\n",
"Tonga Oceania\n",
"Trinidad and Tobago North America\n",
"Tunisia Africa\n",
"Turkey Asia\n",
"Turkmenistan Asia\n",
"Uganda Africa\n",
"Ukraine Europe\n",
"United Arab Emirates Asia\n",
"United Kingdom Europe\n",
"United States North America\n",
"Uruguay South America\n",
"Uzbekistan Asia\n",
"Vanuatu Oceania\n",
"Venezuela South America\n",
"Vietnam Asia\n",
"Palestine Asia\n",
"Zambia Africa\n",
"Zimbabwe Africa\n",
"Name: continent, Length: 175, dtype: object"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 5**\n",
"Oblicz ile osób mieszka na każdym z kontynentów."
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>female_BMI</th>\n",
" <th>male_BMI</th>\n",
" <th>gdp</th>\n",
" <th>population</th>\n",
" <th>under5mortality</th>\n",
" <th>life_expectancy</th>\n",
" <th>fertility</th>\n",
" </tr>\n",
" <tr>\n",
" <th>continent</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Africa</th>\n",
" <td>1259.33213</td>\n",
" <td>1181.44512</td>\n",
" <td>280335.0</td>\n",
" <td>9.772803e+08</td>\n",
" <td>4763.00</td>\n",
" <td>3136.10</td>\n",
" <td>246.50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Asia</th>\n",
" <td>1114.28765</td>\n",
" <td>1084.74598</td>\n",
" <td>1038232.0</td>\n",
" <td>3.949400e+09</td>\n",
" <td>1302.98</td>\n",
" <td>3201.41</td>\n",
" <td>108.48</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Europe</th>\n",
" <td>980.30978</td>\n",
" <td>1009.60877</td>\n",
" <td>1173410.0</td>\n",
" <td>7.219954e+08</td>\n",
" <td>273.60</td>\n",
" <td>2929.30</td>\n",
" <td>59.56</td>\n",
" </tr>\n",
" <tr>\n",
" <th>North America</th>\n",
" <td>526.06040</td>\n",
" <td>497.74034</td>\n",
" <td>338430.0</td>\n",
" <td>5.235798e+08</td>\n",
" <td>392.38</td>\n",
" <td>1412.20</td>\n",
" <td>45.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Oceania</th>\n",
" <td>297.20093</td>\n",
" <td>279.58957</td>\n",
" <td>102803.0</td>\n",
" <td>3.425711e+07</td>\n",
" <td>310.00</td>\n",
" <td>683.20</td>\n",
" <td>34.02</td>\n",
" </tr>\n",
" <tr>\n",
" <th>South America</th>\n",
" <td>322.37322</td>\n",
" <td>308.59791</td>\n",
" <td>139888.0</td>\n",
" <td>3.881582e+08</td>\n",
" <td>288.70</td>\n",
" <td>886.80</td>\n",
" <td>30.10</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" female_BMI male_BMI gdp population \\\n",
"continent \n",
"Africa 1259.33213 1181.44512 280335.0 9.772803e+08 \n",
"Asia 1114.28765 1084.74598 1038232.0 3.949400e+09 \n",
"Europe 980.30978 1009.60877 1173410.0 7.219954e+08 \n",
"North America 526.06040 497.74034 338430.0 5.235798e+08 \n",
"Oceania 297.20093 279.58957 102803.0 3.425711e+07 \n",
"South America 322.37322 308.59791 139888.0 3.881582e+08 \n",
"\n",
" under5mortality life_expectancy fertility \n",
"continent \n",
"Africa 4763.00 3136.10 246.50 \n",
"Asia 1302.98 3201.41 108.48 \n",
"Europe 273.60 2929.30 59.56 \n",
"North America 392.38 1412.20 45.00 \n",
"Oceania 310.00 683.20 34.02 \n",
"South America 288.70 886.80 30.10 "
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 6** Narysyj wykres słupkowy pokazujący ile państw leży na każdym z kontynentów."
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.axes._subplots.AxesSubplot at 0x7f7cceba78d0>"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAE7CAYAAADNbXrqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpY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wMnJTl4VIVJkrbNYs7QHwdcX1U3VNWPgQ8Ax46mLEnStlpMoD8I+Mas/Zv6NknSAFJV2/eFyQnAMVX1on7/BcDjq+r3t3jfWmBtv/sI4NrtL3eb7QN8e4LHm7SW+9dy38D+TbtJ9+/Aqlox35t2WsQBbgYePGt//75tM1W1Dli3iONstyQbqmrVEMeehJb713LfwP5Nu6Xav8UMuXweOCjJQ5LsAjwXOG80ZUmSttV2n6FX1aYkvw98DNgReGdVfXlklUmStslihlyoqo8CHx1RLeMwyFDPBLXcv5b7BvZv2i3J/m33RVFJ0tLirf+S1AgDXZIasagxdEkLl+TRdMtk7DrTVlXvGa6i0Wq9f9OguTH0JPcHDmLzb6pPDVfRaLXcvyQBng88tKpek+QA4Beq6tKBS1u0JH8KPIUu8D5KtwbSZ6rqhCHrGpXW+zctmgr0JC8CTqW7yelK4Ejgc1W1etDCRmQZ9O9twL3A6qp6VP+f1wVV9diBS1u0JF8EDgWuqKpDk+wLvLeqnjpwaSOxDPp3JPAW4FHALnRTte+qqj0GLWwLrY2hnwo8Frixqn4NOBy4fdiSRqr1/j2+ql4M/Aigqm6j++FpwQ+r6l5gU5I9gFvZ/E7radd6//4WOAm4DtgNeBHdarNLSmuB/qOq+hFAkvtU1TV068e
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 7** \n",
" * Kolumna `gdp` zawiera informacje o PKB na obywatela. Stwórz nową kolumnę `gdp_total`, która będzie informować o PKB danego kraju.\n",
" * Oblicz ile wynosi suma światowego PKB (kolumna `gdp_total`).\n",
" * Oblicz ile krajów jest odpowiedzialnych za wytworzenie 80% światego PKB."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 8** Wyświetl wszystkie europejskie państwa, w których oczekiwana długość życia wynosi conajmniej 80 lat. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 9** Znajdź państwo, które ma najbardziej zbliżone PKB do Polski. Spróbuj rozwiązać to zadanie w jednej linijce."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 10 (ostatnie)**\n",
" * Zobacz czy masz zainstalowaną bibliotekę `requests`, która sluży do wykonywania zapytań HTTP. Jeżeli nie, to zainstaluj ją. \n",
" * Uruchom z funkcję `requests.get` podając jako argument link: `https://aws.random.cat/meow`. Wynik zapisz do zmiennej response.\n",
" * Wykonaj metodę `json()` na zmiennej `response` która zwróci Ci słownik, w którym będzie klucz `file`. Zapisz wartość `file` do zmiennej `url`.\n",
" * Zaimportuj `Image` i `display` z biblioteki `from IPython.core.display`.\n",
" * Stwórz obiekt Image podając w konstruktorze zmienną `url`,a wynik zapisz do `image`.\n",
" * Wykonaj funkcję `display` na obiekcie `image`.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}