<jupyter_start><jupyter_text>名目GDP<jupyter_code>import pandas as pd
gdp_org = pd.read_excel("opendata/API_NY.GDP.MKTP.CD_DS2_en_excel_v2_2445330.xls","Data",header=3)
indicator_name = gdp_org["Indicator Name"].unique()[0]
countries = gdp_org.iloc[:,:2]
gdps = gdp_org.iloc[:,4:].stack(dropna=False).reset_index().rename(columns={"level_0":"country_index","level_1":"year",0:indicator_name})
ngdps = pd.merge(countries,gdps,left_index=True,right_on="country_index")
del ngdps["country_index"]
ngdps.rename(columns={indicator_name:"GDP"},inplace=True)
ngdps
# 最新年のGDPが全部NAなら削除
if ngdps.loc[ngdps.year==ngdps.year.max()]["GDP"].isna().all(axis=0):
    ngdps.drop(ngdps.loc[ngdps.year==ngdps.year.max()].index,inplace=True)
latest_year = ngdps["year"].max()
latest_year
# GDPの多い順に表示
ngdps.loc[ngdps.year==latest_year].sort_values("GDP",ascending=False,na_position="last")<jupyter_output><empty_output><jupyter_text>前年比<jupyter_code>ngdps
ngdps["前年比"] = ngdps["GDP"].pct_change()
# 最初の年はNAにする
import numpy as np
ngdps.loc[ngdps.year==ngdps.year.min()] = np.nan
ngdps
world_gdp = ngdps.loc[ngdps["Country Code"]=="WLD"]
world_gdp
import matplotlib.pyplot as plt
%matplotlib notebook
fig = plt.figure(figsize=(9.5,10))
plt.title("全世界の名目GDP")
plt.subplot(2,1,1) # (rows, columns, panel number)
plt.xticks(rotation="90",fontsize=9)
plt.ylabel("1兆$")
plt.yticks(fontsize=9)
plt.grid(axis="y")
chart_data = world_gdp
plt.plot(world_gdp["year"], world_gdp["GDP"]/1000/1000/1000/1000)
plt.plot(["2009","2009"],[10,80], 'k--', lw=0.5)
plt.text("2009",80,"リーマン・ショック(2008.9.15)",fontsize=9, ha="center")

plt.subplot(2,1,2) # (rows, columns, panel number)
plt.title("全世界の名目GDP成長率")
plt.xticks(rotation="90",fontsize=9)
plt.ylabel("%")
plt.yticks(fontsize=9)
plt.grid(axis="y")
chart_data = world_gdp
plt.plot(world_gdp["year"], world_gdp["前年比"]*100)
plt.plot(["2009","2009"],[-2,8], 'k--', lw=0.5)
plt.text("2009",8,"リーマン・ショック(2008.9.15)",fontsize=9, ha="center")

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>国の読み込み<jupyter_code>c_org = pd.read_excel("opendata/API_NY.GDP.MKTP.CD_DS2_en_excel_v2_2445330.xls","Metadata - Countries")
countries = c_org.dropna(subset=["Region","IncomeGroup"],how="any")
countries
regions = c_org.Region.dropna().unique()
regions
c_gdps = pd.merge(countries,ngdps,on="Country Code",how="inner").drop("TableName",axis=1)
#gdps.join(countries,on="Country Code",how="inner")
print(len(countries))
print(len(c_gdps["Country Name"].unique()))<jupyter_output>217
217<jupyter_text>世界TOP10<jupyter_code>gdp_top10_c = pd.DataFrame(c_gdps.loc[c_gdps.year==latest_year].sort_values("GDP", ascending=False).head(10)[["Country Code","Country Name"]])
c_gdps_top10 = c_gdps[c_gdps["Country Code"].isin(gdp_top10_c["Country Code"])]
c_gdps.iloc[gdp_top10_c.index]

fig = plt.figure(figsize=(9.5,10))
plt.subplot(2,1,1) # (rows, columns, panel number)
plt.title("名目GDP TOP10")
plt.xticks(rotation="90",fontsize=9)
plt.ylabel("1000億US$")
plt.yticks(fontsize=9)
plt.grid(axis="y")
chart_data = c_gdps_top10
years = chart_data.year.unique()
for c in gdp_top10_c["Country Name"]:
    plt.plot(years, chart_data.loc[chart_data["Country Name"]==c,"GDP"]/1000/1000/1000/100,label=c)
plt.plot(["2009","2009"],[10,175], 'k--', lw=0.5)
plt.text("2009",175,"リーマン・ショック(2008.9.15)",fontsize=9, ha="center")
plt.legend()

plt.subplot(2,1,2) # (rows, columns, panel number)
plt.title("名目GDP TOP10 前年伸び率")
plt.xticks(rotation="90",fontsize=9)
plt.ylabel("%")
plt.yticks(fontsize=9)
plt.ylim(-25,25)
plt.grid(axis="y")
chart_data = c_gdps_top10
years = chart_data.year.unique()
for c in gdp_top10_c["Country Name"]:
    plt.plot(years, chart_data.loc[chart_data["Country Name"]==c,"前年比"]*100,label=c)
plt.plot(["2009","2009"],[-25,20], 'k--', lw=0.5)
plt.text("2009",20,"リーマン・ショック(2008.9.15)",fontsize=9, ha="center")
plt.legend()

plt.tight_layout()<jupyter_output><empty_output><jupyter_text>日本<jupyter_code>jpn_gdp = c_gdps.loc[c_gdps["Country Code"]=="JPN"]
jpn_gdp.tail(10)<jupyter_output><empty_output><jupyter_text>人口あたりの計算  人口の読み込み<jupyter_code>pop_org = pd.read_excel("population/opendata/world/WPP2019_POP_F01_1_TOTAL_POPULATION_BOTH_SEXES.xlsx",header=16)
pop_org.head(10)
#cnt_org = pd.read_excel("world/country-code-2020-iso-obp.xlsx")
#cnt_org.head(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20465657741559953, 'fraction_numerical': 0.06263096623981373, 'mean_word_length': 9.793969849246231, 'pattern_counts': {'":': 2, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '169055', 'license': 'MIT', 'n_tokens_mistral': 2155, 'n_tokens_neox': 1966, 'n_words': 187, 'path': 'visualize_nominal_gdp.ipynb', 'repo_name': 'marzg510/data-analyze-economy', 'size': '4295'}