~piotr-machura/scientific-python

d1b44b4ede9fa1982f580da8ffab06b54072a8ed — Piotr Machura 9 months ago 082baa8
Updated pandas exercise
2 files changed, 56 insertions(+), 24 deletions(-)

M README.md
M scientific_python/natural_resources.py
M README.md => README.md +47 -23
@@ 315,32 315,56 @@ Simple data exploration in [Pandas](https://pandas.pydata.org/).
```
$ poetry run natural_resources
Mean:
               Gas production  ... Oil net imports per capita
                         mean  ...                       mean
Entity                         ...
Afghanistan      7.890248e+08  ...                  -0.000783
Albania          1.266962e+08  ...                  -0.067123
Algeria          5.431857e+10  ...                  -0.792465
American Samoa   0.000000e+00  ...                   0.000000
Angola           7.812592e+08  ...                  -2.256367
...                       ...  ...                        ...
Western Sahara   0.000000e+00  ...                   0.000000
World            2.139833e+12  ...                   0.007340
Yemen            1.118596e+09  ...                  -0.324281
Zambia           0.000000e+00  ...                   0.063354
Zimbabwe         0.000000e+00  ...                   0.000000
               Gas production Gas consumption  ... Coal net imports per capita Oil net imports per capita
                         mean            mean  ...                        mean                       mean
Entity                                         ...
Afghanistan      7.890248e+08    2.725105e+08  ...                    0.000000                  -0.000783
Albania          1.266962e+08    1.266962e+08  ...                    0.031471                  -0.067123
Algeria          5.431857e+10    2.016324e+10  ...                    0.016863                  -0.792465
American Samoa   0.000000e+00    0.000000e+00  ...                    0.000000                   0.000000
Angola           7.812592e+08    4.663816e+08  ...                    0.000000                  -2.256367
...                       ...             ...  ...                         ...                        ...
Western Sahara   0.000000e+00    0.000000e+00  ...                    0.000000                   0.000000
World            2.139833e+12    2.146542e+12  ...                   -0.001034                   0.007340
Yemen            1.118596e+09    1.363676e+08  ...                    0.001507                  -0.324281
Zambia           0.000000e+00    0.000000e+00  ...                    0.001488                   0.063354
Zimbabwe         0.000000e+00    0.000000e+00  ...                   -0.003984                   0.000000

[231 rows x 43 columns]

Entity   Year
Germany  1991    1.309
         1992    1.446
         ...     ...
         2018    1.202
         2019    1.210
         2020    1.152
         2021    0.000
Name: Oil net imports per capita, dtype: float64
              Coal consumption per capita  Coal imports per capita  Coal production per capita
Entity  Year
Germany 1991                        4.654                   0.2372                       4.432
        1992                        4.111                   0.2388                       3.926
        1993                        3.787                   0.1999                       3.556
        1994                        3.540                   0.2240                       3.275
        1995                        3.340                   0.2118                       3.101
        1996                        3.312                   0.2250                       2.956
        1997                        3.164                   0.2725                       2.806
        1998                        2.997                   0.2972                       2.597
        1999                        2.854                   0.3039                       2.520
        2000                        2.932                   0.3654                       2.519
        2001                        2.982                   0.4359                       2.529
        2002                        3.029                   0.4103                       2.588
        2003                        3.050                   0.4261                       2.547
        2004                        3.068                   0.4844                       2.585
        2005                        2.959                   0.4282                       2.524
        2006                        2.979                   0.4867                       2.456
        2007                        3.079                   0.5320                       2.517
        2008                        2.934                   0.5155                       2.399
        2009                        2.779                   0.4356                       2.285
        2010                        2.850                   0.4883                       2.270
        2011                        2.903                   0.5299                       2.343
        2012                        3.039                   0.5409                       2.433
        2013                        3.042                   0.6305                       2.352
        2014                        2.947                   0.7044                       2.290
        2015                        2.930                   0.6886                       2.259
        2016                        2.825                   0.6711                       2.137
        2017                        2.698                   0.5896                       2.119
        2018                        2.601                   0.5441                       2.033
        2019                        2.051                   0.4951                       1.572
        2020                        1.644                   0.3569                       1.282
        2021                        0.000                   0.0000                       0.000

Does Germany import coal when the consumption increases?
Pearson correltion: 0.04456, p-value 0.81185

M scientific_python/natural_resources.py => scientific_python/natural_resources.py +9 -1
@@ 19,7 19,15 @@ def main():
    germany = df.query("Entity == 'Germany'")
    group = germany.groupby(["Entity", "Year"])
    print("")
    print(group["Oil net imports per capita"].mean())
    print(
        group[
            [
                "Coal consumption per capita",
                "Coal imports per capita",
                "Coal production per capita",
            ]
        ].mean()
    )

    print("\nDoes Germany import coal when the consumption increases?")
    pearson, p_value = pearsonr(