Yosuke Tsuchiya - Fab Futures - Data Science
Home About

Jupyter Tutorial¶

This is a tutorial book of Jupyter and Python.

Python Sample¶

Here is the first code of Python

In [1]:
print("Hello, World")
Hello, World
In [4]:
a = 1
b = 10

c = a + b

ほら、こんなに簡単にmarkdownが書けるよ

No description has been provided for this image

CSVを読み込むやり方¶

もっとも手っ取り早く、データ分析の用途の場合はPandaを使うのが速いし楽

In [5]:
import pandas as pd

# df: DataFrameの略
df = pd.read_csv('./datasets/sdg_index_2000-2022.csv')

df
Out[5]:
country_code country year sdg_index_score goal_1_score goal_2_score goal_3_score goal_4_score goal_5_score goal_6_score ... goal_8_score goal_9_score goal_10_score goal_11_score goal_12_score goal_13_score goal_14_score goal_15_score goal_16_score goal_17_score
0 AFG Afghanistan 2000 36.0 28.8 27.3 19.2 1.6 20.8 32.4 ... 38.5 5.2 0.0 25.8 94.7 99.4 0.0 51.9 39.2 34.2
1 AFG Afghanistan 2001 36.3 28.8 30.6 19.4 1.6 20.8 32.4 ... 38.5 5.2 0.0 25.8 94.5 99.4 0.0 51.9 39.2 34.2
2 AFG Afghanistan 2002 36.3 28.8 30.7 19.7 1.6 20.8 32.7 ... 38.4 5.2 0.0 26.1 94.1 99.4 0.0 51.8 39.2 34.2
3 AFG Afghanistan 2003 36.7 28.8 32.5 19.9 1.6 20.8 33.0 ... 38.4 5.2 0.0 26.5 94.4 99.4 0.0 51.8 39.2 34.2
4 AFG Afghanistan 2004 37.1 28.8 32.1 21.1 1.6 20.8 33.3 ... 38.5 5.2 0.0 26.8 94.8 99.4 0.0 51.8 39.2 34.2
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
4135 ZWE Zimbabwe 2018 55.9 26.4 46.6 39.8 57.9 76.7 51.6 ... 63.8 23.5 32.4 79.2 94.6 94.1 0.0 76.4 45.5 45.6
4136 ZWE Zimbabwe 2019 53.7 21.4 46.0 40.6 60.6 77.0 51.3 ... 62.8 23.8 24.0 58.6 94.6 94.2 0.0 76.4 44.0 44.5
4137 ZWE Zimbabwe 2020 54.3 20.1 47.2 41.5 62.0 77.0 51.1 ... 62.9 25.4 24.0 60.0 94.6 94.3 0.0 76.4 44.6 46.0
4138 ZWE Zimbabwe 2021 54.8 20.8 48.4 41.9 62.5 77.1 51.1 ... 63.1 27.0 24.0 61.2 94.5 94.3 0.0 77.9 43.1 48.1
4139 ZWE Zimbabwe 2022 55.6 21.5 48.5 41.9 62.5 77.2 51.1 ... 63.2 27.0 24.0 65.7 94.5 94.3 0.0 77.9 40.5 47.8

4140 rows × 21 columns

古典的というかスタンダードなやり方¶

地道ファイルを開いて読み込む方法ですが、めんどい。

In [ ]:
import csv

with open('./datasets/sdg_index_2000-2022.csv') as f:
    reader = csv.reader(f)
    rows = [row for row in reader]

for item in rows:
    print(item[1])
    

Matplotlibで可視化¶

In [19]:
import matplotlib.pyplot as plt
In [20]:
jpn = df.query('country == "Japan"')
jpn
Out[20]:
country_code country year sdg_index_score goal_1_score goal_2_score goal_3_score goal_4_score goal_5_score goal_6_score ... goal_8_score goal_9_score goal_10_score goal_11_score goal_12_score goal_13_score goal_14_score goal_15_score goal_16_score goal_17_score
1840 JPN Japan 2000 75.3 99.7 73.2 87.5 95.6 53.1 83.6 ... 85.9 78.7 77.8 89.4 58.1 69.5 52.6 58.0 85.3 62.2
1841 JPN Japan 2001 75.6 99.7 72.7 89.1 95.6 53.2 83.9 ... 85.8 80.0 77.8 89.4 58.6 69.8 53.0 58.0 85.3 62.5
1842 JPN Japan 2002 75.7 99.7 72.6 89.6 95.6 53.3 84.2 ... 85.6 81.7 77.8 89.4 58.6 69.4 53.4 57.9 85.3 62.6
1843 JPN Japan 2003 75.8 99.7 70.9 90.0 95.6 53.3 84.2 ... 85.7 82.5 77.8 89.4 58.6 69.3 55.2 57.7 85.3 62.8
1844 JPN Japan 2004 76.1 99.7 72.5 90.4 95.6 53.4 84.3 ... 86.0 84.9 77.8 89.4 58.7 69.4 55.5 57.8 85.5 62.8
1845 JPN Japan 2005 76.5 99.7 73.2 91.6 95.6 54.6 84.3 ... 86.1 87.0 77.8 89.4 58.9 69.3 55.4 58.6 85.7 62.9
1846 JPN Japan 2006 76.6 99.7 72.5 91.8 95.6 54.9 84.4 ... 86.3 87.8 77.8 89.4 59.1 69.6 56.0 58.8 85.9 62.8
1847 JPN Japan 2007 76.7 99.7 73.3 91.9 95.6 54.9 84.4 ... 86.4 88.3 77.8 88.3 59.6 69.2 55.7 58.7 86.9 63.0
1848 JPN Japan 2008 76.6 99.7 73.7 91.4 95.6 54.9 84.5 ... 86.3 88.3 77.8 84.5 59.7 70.1 55.5 58.7 87.0 63.4
1849 JPN Japan 2009 76.9 99.7 72.8 91.2 95.6 55.9 84.6 ... 85.8 88.8 77.8 85.6 60.6 71.0 54.9 58.7 88.2 64.6
1850 JPN Japan 2010 77.1 99.7 72.4 92.6 95.6 55.9 84.6 ... 85.7 89.8 81.6 86.2 60.1 70.4 53.3 59.0 87.2 65.4
1851 JPN Japan 2011 77.4 99.6 73.3 93.1 95.6 56.2 84.7 ... 86.0 92.4 81.6 86.7 59.5 68.1 53.9 59.1 87.5 67.7
1852 JPN Japan 2012 77.6 99.4 74.4 93.0 95.6 55.1 84.7 ... 86.1 92.5 81.6 87.1 59.8 67.5 54.3 60.6 88.8 68.1
1853 JPN Japan 2013 77.7 99.3 74.4 92.9 95.6 57.2 84.7 ... 86.3 94.3 80.5 83.8 60.2 67.3 56.9 63.0 84.8 68.1
1854 JPN Japan 2014 77.7 99.3 74.8 92.9 95.1 57.7 84.8 ... 86.6 94.3 80.5 85.2 60.3 67.3 54.9 63.2 84.7 68.0
1855 JPN Japan 2015 78.1 99.3 74.9 93.5 94.9 58.4 84.8 ... 88.0 93.8 80.5 82.5 60.9 70.5 54.0 63.3 88.8 67.6
1856 JPN Japan 2016 78.0 99.3 74.6 93.7 94.5 58.8 84.8 ... 88.2 92.0 80.5 85.1 60.7 70.5 52.9 63.3 88.6 67.2
1857 JPN Japan 2017 78.1 99.3 74.5 93.9 94.1 59.7 84.9 ... 88.2 93.9 80.5 83.4 60.8 71.3 53.1 63.3 87.3 67.7
1858 JPN Japan 2018 78.5 99.3 74.6 93.8 94.0 60.3 84.9 ... 88.7 94.0 80.5 83.5 60.9 72.6 53.2 63.7 87.6 70.6
1859 JPN Japan 2019 78.8 99.3 74.8 94.0 94.2 60.9 84.9 ... 88.3 94.5 80.5 84.8 60.9 73.1 55.6 63.5 86.9 70.9
1860 JPN Japan 2020 79.0 99.3 74.6 94.4 93.9 60.9 84.9 ... 87.8 94.6 80.5 85.8 60.9 73.9 55.7 63.3 87.1 73.0
1861 JPN Japan 2021 79.5 99.3 75.2 94.4 93.9 61.2 84.9 ... 88.1 94.4 80.5 86.9 66.1 73.5 55.8 63.5 87.4 74.3
1862 JPN Japan 2022 79.4 99.3 75.2 94.6 93.9 61.7 84.9 ... 88.2 94.4 80.5 84.2 66.1 73.5 55.8 63.1 86.3 75.6

23 rows × 21 columns

In [25]:
plt.figure(figsize=(10,6))
chn = df.query('country == "China"')

plt.plot(jpn['year'],jpn['sdg_index_score'],label="SDGs Score in Japan",color='black')
plt.plot(chn['year'],chn['sdg_index_score'],label="SDGs Score in China",color='red')


plt.ylim(bottom=-0.1)
plt.ylim(top=100)
#plt.tight_layout()
plt.show()
No description has been provided for this image
In [ ]: