api_calls (1)
Dependence on Russian and Ukrainian Imports
As the world is changing this week, we can try to understand how important Russian and Ukrainian imports are to the US.
Copyright By Assignmentchef assignmentchef
Census.Gov has international trade data exposed for us to download.
Please pay particular attention to the example calls under Monthly International Trade Time Series Imports.
Task 1 Understanding the API behavior
Please repeat both example API calls on the website using request.get() and make sure you get sensisble results. Hint: what is the length of the params we will pass to the API?
import requests
base_url = http://api.census.gov/data/timeseries/intltrade/imports/enduse?
params = {get: CTY_CODE,CTY_NAME,I_ENDUSE,I_ENDUSE_LDESC,GEN_VAL_MO,CON_VAL_MO,
time: 2013-01}
resp = requests.get(base_url, params=params)
resp.status_code
dat = resp.json()
len(dat[10])
[[CTY_CODE,
CTY_NAME,
I_ENDUSE,
I_ENDUSE_LDESC,
GEN_VAL_MO,
CON_VAL_MO,
FINLAND,
TOTAL IMPORTS FOR ALL END-USE CODES,
319554327,
335786013,
2013-01],
KYRGYZSTAN,
TOTAL IMPORTS FOR ALL END-USE CODES,
2013-01]]
import pandas as pd
df = pd.DataFrame(dat[1:], columns=dat[0])
NameError Traceback (most recent call last)
Input In [1], in
1 import pandas as pd
-> 3 df = pd.DataFrame(dat[1:], columns=dat[0])
NameError: name dat is not defined
(13043, 7)
df.head(2)
CTY_CODECTY_NAMEI_ENDUSEI_ENDUSE_LDESCGEN_VAL_MOCON_VAL_MOtime
04050FINLANDTOTAL IMPORTS FOR ALL END-USE CODES3195543273357860132013-01
14635KYRGYZSTANTOTAL IMPORTS FOR ALL END-USE CODES17592175922013-01
Task 2 Get more data
Please get the general imports value for all end-use codes for 2013-2020 for the months 01-12. Hint: you should be able to modify the example calls.
Please combine all of the data into one data frame
# Shrinking the years so Ed doesnt crash
years = list(range(2018, 2021))
months = range(1, 13)
for year in years:
print(year)
for month in months:
# year = 2020
# month = 01
month = month if month >= 10 else 0 + str(month)
base_url = http://api.census.gov/data/timeseries/intltrade/imports/enduse?
params = {get: CTY_CODE,CTY_NAME,I_ENDUSE,I_ENDUSE_LDESC,GEN_VAL_MO,CON_VAL_MO,
time: {YEAR}-{MONTH}.format(YEAR=year, MONTH=month)}
resp = requests.get(base_url, params=params)
dat = resp.json()
df = pd.DataFrame(dat[1:], columns=dat[0])
dfs.append(df)
bdf = pd.concat(dfs)
bdf.tail(3)
CTY_CODECTY_NAMEI_ENDUSEI_ENDUSE_LDESCGEN_VAL_MOCON_VAL_MOtime
190973070VENEZUELA50040OTHER (MOVIES, MISCELLANEOUS IMPORTS, AND SPEC002020-12
190983120GUYANA50040OTHER (MOVIES, MISCELLANEOUS IMPORTS, AND SPEC78669786692020-12
190993150SURINAME50040OTHER (MOVIES, MISCELLANEOUS IMPORTS, AND SPEC75674756742020-12
Task 3 Analyze the Dependence
Please quantify a major dependence we have on Russia or Ukrain using the data weve collected.
sdf = bdf_grp.get_group(grp)
from_russian = sdf.CTY_NAME == RUSSIA
russia_val = sdf.loc[from_russian, GEN_VAL_MO]
russia_val.astype(float).sum()
bdf_grp = bdf.groupby(I_ENDUSE_LDESC)
prop_russia = {}
for grp in bdf_grp.groups:
sdf = bdf_grp.get_group(grp)
from_russian = sdf.CTY_NAME == RUSSIA
russia_val = sdf.loc[from_russian, GEN_VAL_MO].astype(float).sum()
prop_russia.update({grp: russia_val / sdf.GEN_VAL_MO.astype(float).sum()})
sorted([(k, v) for k, v in prop_russia.items()], key=lambda x: x[1])[-10:]
[(NONFERROUS METALS, OTHER, 0.008325050758562219),
(OTHER MILITARY EQUIPMENT, 0.008895039279254493),
(PLYWOOD AND VENEERS, 0.00986277801523884),
(NICKEL, 0.01232060479631816),
(CHEMICALS-FERTILIZERS, 0.014133116188763358),
(OTHER PRECIOUS METALS, 0.021334977908361304),
(STEELMAKING MATERIALS, 0.027548659235540184),
(NUCLEAR FUEL MATERIALS, 0.038887036518478385),
(FUEL OIL, 0.05804145846671529),
(SPACECRAFT, EXCLUDING MILITARY, 0.1342180905408842)]
CS: assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.