beautiful soup - turning attributes into dataframe - BEA API
up vote
0
down vote
favorite
I'm attempting to use the BEA's API to query income data. API Instructions - https://apps.bea.gov/api/_pdf/bea_web_service_api_user_guide.pdf
My goal is to parse the XML generated and turn it into a dataframe, with columns for the different years.
The issue that I run into is that the way I am parsing the data, it is in a "melted" format, where I want individual columns for the years and the Income data for those years in each of those columns.
How can I accomplish this? Below is the code that I am using. It requires that you sign up for an API key via email and enter it after "UserID" in the URL below.
bea_income = 'https://apps.bea.gov/api/data/?UserID=ENTERYOURAPIKEY&method=GetData&'
'datasetname=RegionalIncome&TableName=RPI2&LineCode=2&Year=2014,2015,2016&GeoFips=MSA&ResultFormat=xml'
bea_inc_request = requests.get(bea_income, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'})
bea_inc_html = bea_inc_request.content
bea_inc_soup = BeautifulSoup(bea_inc_html, 'xml')
MSA =
TimePeriod =
Income =
GeoFips =
for i in range(len(bea_inc_soup.Results.find_all('Data'))):
MSA.append(bea_inc_soup.Results.find_all('Data')[i]['GeoName'])
GeoFips.append(bea_inc_soup.Results.find_all('Data')[i]['GeoFips'])
Income.append(bea_inc_soup.Results.find_all('Data')[i]['DataValue'])
TimePeriod.append(bea_inc_soup.Results.find_all('Data')[i]['TimePeriod'])
income_data = pd.DataFrame({'MSA':MSA, 'FIPS':GeoFips, 'Year':TimePeriod, 'Income':Income})
MSA FIPS Year Income
0 Abilene, TX (Metropolitan Statistical Area) 10180 2014 41818
1 Abilene, TX (Metropolitan Statistical Area) 10180 2015 41651
2 Abilene, TX (Metropolitan Statistical Area) 10180 2016 40409
3 Akron, OH (Metropolitan Statistical Area) 10420 2016 45448
4 Akron, OH (Metropolitan Statistical Area) 10420 2015 45298
python pandas beautifulsoup
add a comment |
up vote
0
down vote
favorite
I'm attempting to use the BEA's API to query income data. API Instructions - https://apps.bea.gov/api/_pdf/bea_web_service_api_user_guide.pdf
My goal is to parse the XML generated and turn it into a dataframe, with columns for the different years.
The issue that I run into is that the way I am parsing the data, it is in a "melted" format, where I want individual columns for the years and the Income data for those years in each of those columns.
How can I accomplish this? Below is the code that I am using. It requires that you sign up for an API key via email and enter it after "UserID" in the URL below.
bea_income = 'https://apps.bea.gov/api/data/?UserID=ENTERYOURAPIKEY&method=GetData&'
'datasetname=RegionalIncome&TableName=RPI2&LineCode=2&Year=2014,2015,2016&GeoFips=MSA&ResultFormat=xml'
bea_inc_request = requests.get(bea_income, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'})
bea_inc_html = bea_inc_request.content
bea_inc_soup = BeautifulSoup(bea_inc_html, 'xml')
MSA =
TimePeriod =
Income =
GeoFips =
for i in range(len(bea_inc_soup.Results.find_all('Data'))):
MSA.append(bea_inc_soup.Results.find_all('Data')[i]['GeoName'])
GeoFips.append(bea_inc_soup.Results.find_all('Data')[i]['GeoFips'])
Income.append(bea_inc_soup.Results.find_all('Data')[i]['DataValue'])
TimePeriod.append(bea_inc_soup.Results.find_all('Data')[i]['TimePeriod'])
income_data = pd.DataFrame({'MSA':MSA, 'FIPS':GeoFips, 'Year':TimePeriod, 'Income':Income})
MSA FIPS Year Income
0 Abilene, TX (Metropolitan Statistical Area) 10180 2014 41818
1 Abilene, TX (Metropolitan Statistical Area) 10180 2015 41651
2 Abilene, TX (Metropolitan Statistical Area) 10180 2016 40409
3 Akron, OH (Metropolitan Statistical Area) 10420 2016 45448
4 Akron, OH (Metropolitan Statistical Area) 10420 2015 45298
python pandas beautifulsoup
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm attempting to use the BEA's API to query income data. API Instructions - https://apps.bea.gov/api/_pdf/bea_web_service_api_user_guide.pdf
My goal is to parse the XML generated and turn it into a dataframe, with columns for the different years.
The issue that I run into is that the way I am parsing the data, it is in a "melted" format, where I want individual columns for the years and the Income data for those years in each of those columns.
How can I accomplish this? Below is the code that I am using. It requires that you sign up for an API key via email and enter it after "UserID" in the URL below.
bea_income = 'https://apps.bea.gov/api/data/?UserID=ENTERYOURAPIKEY&method=GetData&'
'datasetname=RegionalIncome&TableName=RPI2&LineCode=2&Year=2014,2015,2016&GeoFips=MSA&ResultFormat=xml'
bea_inc_request = requests.get(bea_income, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'})
bea_inc_html = bea_inc_request.content
bea_inc_soup = BeautifulSoup(bea_inc_html, 'xml')
MSA =
TimePeriod =
Income =
GeoFips =
for i in range(len(bea_inc_soup.Results.find_all('Data'))):
MSA.append(bea_inc_soup.Results.find_all('Data')[i]['GeoName'])
GeoFips.append(bea_inc_soup.Results.find_all('Data')[i]['GeoFips'])
Income.append(bea_inc_soup.Results.find_all('Data')[i]['DataValue'])
TimePeriod.append(bea_inc_soup.Results.find_all('Data')[i]['TimePeriod'])
income_data = pd.DataFrame({'MSA':MSA, 'FIPS':GeoFips, 'Year':TimePeriod, 'Income':Income})
MSA FIPS Year Income
0 Abilene, TX (Metropolitan Statistical Area) 10180 2014 41818
1 Abilene, TX (Metropolitan Statistical Area) 10180 2015 41651
2 Abilene, TX (Metropolitan Statistical Area) 10180 2016 40409
3 Akron, OH (Metropolitan Statistical Area) 10420 2016 45448
4 Akron, OH (Metropolitan Statistical Area) 10420 2015 45298
python pandas beautifulsoup
I'm attempting to use the BEA's API to query income data. API Instructions - https://apps.bea.gov/api/_pdf/bea_web_service_api_user_guide.pdf
My goal is to parse the XML generated and turn it into a dataframe, with columns for the different years.
The issue that I run into is that the way I am parsing the data, it is in a "melted" format, where I want individual columns for the years and the Income data for those years in each of those columns.
How can I accomplish this? Below is the code that I am using. It requires that you sign up for an API key via email and enter it after "UserID" in the URL below.
bea_income = 'https://apps.bea.gov/api/data/?UserID=ENTERYOURAPIKEY&method=GetData&'
'datasetname=RegionalIncome&TableName=RPI2&LineCode=2&Year=2014,2015,2016&GeoFips=MSA&ResultFormat=xml'
bea_inc_request = requests.get(bea_income, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'})
bea_inc_html = bea_inc_request.content
bea_inc_soup = BeautifulSoup(bea_inc_html, 'xml')
MSA =
TimePeriod =
Income =
GeoFips =
for i in range(len(bea_inc_soup.Results.find_all('Data'))):
MSA.append(bea_inc_soup.Results.find_all('Data')[i]['GeoName'])
GeoFips.append(bea_inc_soup.Results.find_all('Data')[i]['GeoFips'])
Income.append(bea_inc_soup.Results.find_all('Data')[i]['DataValue'])
TimePeriod.append(bea_inc_soup.Results.find_all('Data')[i]['TimePeriod'])
income_data = pd.DataFrame({'MSA':MSA, 'FIPS':GeoFips, 'Year':TimePeriod, 'Income':Income})
MSA FIPS Year Income
0 Abilene, TX (Metropolitan Statistical Area) 10180 2014 41818
1 Abilene, TX (Metropolitan Statistical Area) 10180 2015 41651
2 Abilene, TX (Metropolitan Statistical Area) 10180 2016 40409
3 Akron, OH (Metropolitan Statistical Area) 10420 2016 45448
4 Akron, OH (Metropolitan Statistical Area) 10420 2015 45298
python pandas beautifulsoup
python pandas beautifulsoup
asked Nov 5 at 3:52
steich
467
467
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148088%2fbeautiful-soup-turning-attributes-into-dataframe-bea-api%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password