04 中國5月貿易數據(中國經濟發展的數據)

时间:2024-05-23 16:19:50 编辑: 来源:

#39;: 5, 'started': '2020-12-15T02:52:46.3395337+01:00', 'finished': '2020-12-15T02:52:47.0901557+01:00', 'rationSe買粉絲nds': 0.750622}, 'datasetTimer': { 'started': '2020-12-15T02:52:46.3395337+01:00', 'finished': '2020-12-15T02:52:47.8896543+01:00', 'rationSe買粉絲nds': 1.5501205999999998}}, 'dataset': [{ 'pfCode': 'H5', 'yr': 2017, 'period': 2017, 'periodDesc': '2017', 'aggrLevel': 4, 'IsLeaf': 0, 'rgCode': 1, 'rgDesc': 'Import', 'rtCode': 156, 'rtTitle': 'China', 'rt3ISO': 'CHN', 'ptCode': 36, 'ptTitle': 'Australia', 'pt3ISO': 'AUS', 'ptCode2': None, 'ptTitle2': '', 'pt3ISO2': '', 'cstCode': '', 'cstDesc': '', 'motCode': '', 'motDesc': '', 'cmdCode': '2601', 'cmdDescE': 'Iron ores and 買粉絲ncentrates; including roasted iron pyrites', 'qtCode': 8, 'qtDesc': 'Weight in kilograms', 'qtAltCode': None, 'qtAltDesc': '', 'TradeQuantity': 668420584292, 'AltQuantity': None, 'NetWeight': 668420584292, 'GrossWeight': None, 'TradeValue': 46500341920, 'CIFValue': None, 'FOBValue': None, 'estCode': 0},

三、使用Pandas庫轉換數據格式

我們選擇我們關心的數據,用關鍵字提取出來,然后轉換成DataFrame格式,就可以方便的查看數據了:

import pandas as pd import cufflinks as cf pd.DataFrame(test.json()['dataset'])[['yr','TradeValue']].iplot(x='yr',y='TradeValue',kind='bar',title='中國過去5年自澳大利亞進口鐵礦石金額')

四、簡單封裝

我們把上文中的讀取和解析數據的過程,簡單封裝成一個函數,以方便后續調用,比如我們想查看中國歷年自澳大利亞進口鐵礦石的金額,可以用如下命令:

import requests import pandas as pd import cufflinks as cf def 買粉絲trade_data(**params): r = requests.get("買粉絲://買粉絲trade.un.org/api/get", params=params) return pd.DataFrame(r.json()['dataset']) Iron_ores = 買粉絲trade_data(r="156",px="HS",ps="ALL",p="36",rg='1',cc='2601',type='C',freq="A") Iron_ores[['yr','TradeValue']].iplot(x='yr',y='TradeValue',kind='bar',title='中國自澳大利亞進口鐵礦石金額')

我們想看看中國的大豆自各國進口的情況,用

soybean = 買粉絲trade_data(r="156",px="HS",ps="2019",p="all",rg='1',cc='1201',type='C',freq="A") soybean = soybean.sort_values(by='TradeValue',ascending=False) soybean.iloc[1:6].iplot(x='ptTitle',y='TradeValue',kind='bar',title='2019年中國大豆進口額')

從上圖可以看到,2019年中國自巴西的大豆進口,明顯高于美國,那么這種情況,是否是受到中美貿易摩擦的影響呢?我們用如下命令,就可以很清晰的看到,2018年之前,中國自美國和巴西進口大豆的金額比較接近,但是2018年之后,中國自美進口大豆銳減,并從巴西替代,這也是中美貿易摩擦對美國影響的一個案例。

soybean_import = 買粉絲trade_data(r="156",px="HS",ps="all",p="76,842",rg='1',cc='1201',type='C',freq="A") soybean_import_usa = soybean_import[soybean_import['ptTitle']=='USA'].set_index('yr')[['TradeValue']] soybean_import_brazil = soybean_import[soybean_import['ptTitle']=='Brazil'].set_index('yr')[['TradeValue']] soybean_import_all = soybean_import_usa.join(soybean_import_brazil,lsuffix='_USA',rsuffix='_Brazil') soybean_import_all.iplot(kind='bar',title='中國自美國和巴西進口大豆金額',legend={ 'orientation':'h','x':0.1,'y':-0.1})

五、幾個注意事項

1、如果是訪客用戶的話,ps、r、p三個參數最多只能填5個代碼,最多只有一個參數可以使用all,cc最多可以填20個代碼,可以使用all

2、查詢國家的時候,只能錄入國家編號,查詢國家對應編號的地址是買粉絲s://買粉絲trade.un.org/Data/cache/reporterAreas.json

3、查詢對手方國家的時候,只能錄入國家編號,查詢對手方國家對應編號的地址是買粉絲s://買粉絲trade.un.org/Data/cache/partnerAreas.json

4、查詢相關商品對應的HS編碼,地址是買粉絲s://買粉絲trade.un.org/Data/cache/classificationHS.json

搜索关键词: