Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
FAOproject/LoadCreateFAODataset.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
66 lines (61 sloc)
2.69 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""================================================================================================= | |
Example of FAO Domestic Animal Diversity Information System (DAD-IS) | |
host:https://us-central1-dadis-ws.cloudfunctions.net/exportMultipleDataToCSV? | |
full syntax | |
host + 'typeOfData=' + typeOfData | |
+ '&startYear=' + startYear | |
+ '&endYear=' + endYear | |
+ '&iso3=' + country | |
+ "&specie=" + specie | |
+ "&breed=" + breed | |
+ '&lang=' + LANG, | |
TypeOfData = [ 'metadata', 'population', 'conservation', 'SDG251', 'SDG252'] | |
Lang = ['en', 'es', 'fr' ] | |
Any fields can be omitted except typeOfData | |
Creates Pandas Dataframa | |
Jason Graham 4/14/2021 | |
=================================================================================================""" | |
import requests | |
import json | |
import pandas as pd | |
import io | |
""" | |
convert bytes into string | |
""" | |
def ByteStr(content): | |
my_str = content # b means its a byte string | |
new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding | |
# print(new_str) | |
return new_str | |
""" | |
Create Pandas DataFrame | |
""" | |
def MakeDataframe(cont_str): | |
df = io.StringIO(cont_str) | |
print(df) | |
df = pd.read_csv(df, sep=",", skiprows=3, header=None) | |
df = df.drop(columns=[21]) | |
df = df.rename(columns={0: "Country", 1: "ISO3", 2: "Specie", 3: "Breed", 4: "Year", 5: "Population min", 6: "Population max", | |
7: "Trend", 8: "Population figured based on", 9: "Breeding male", 10: "Breeding female", | |
11: "Females registered in herdbooks", 12: "Females bred pure", 13: "Herds", 14: "Herds size", | |
15: "AI used", 16: "Male in AI", 17: "In situ conservation programmes in place", 18: "Reliability", | |
19: "Last update", 20: "Geographical classification"}) | |
return df | |
# -------------------------------------------------------------------------------------------------- | |
# | |
# -------------------------------------------------------------------------------------------------- | |
if __name__ == '__main__': | |
databases = {} | |
date = "4.14.2021" | |
url = 'https://us-central1-dadis-ws.cloudfunctions.net/exportMultipleDataToCSV' | |
param = {'typeOfData':'population', | |
'startYear': 0, | |
'endYear': 2020, | |
'specie': 'Cattle', | |
'iso3':''} | |
response = requests.get(url, param) # pulling dataset from http | |
contento = response.content | |
cont_str = ByteStr(contento) # turning byte class into a string | |
dataframe = MakeDataframe(cont_str) #Creating Pandas Dataframe | |
print(dataframe) | |
dataframe.to_csv(r'C:/Users/jason/OneDrive - purdue.edu/SQLite_DataBase_FAOProject/FAO_DataFrame_' + date +".csv", index=False) #writing dataframe to csv |