본문 바로가기
  • Homines, dum docent, discunt
  • Repetitio est Mater Memoriae
  • Dilige et fac quod vis
지식 창고/파이썬

미국 클러스터 데이터 가지고 오기 _1편

by Manana Cho 2023. 4. 13.
반응형

About Cluster Mapping US

Cluster Mapping US는 미국 산업 클러스터에 대한 선별 및 지속적으로 업데이트 된 데이터를 공개로 제공하는 프로젝트이고 JSON 데이터를 반환하는 API를 구축하여 HTTP 요청을 통해 액세스할 수 있습니다. 이 프로젝트는 미국 경제 개발국과 하버드 비즈니스 스쿨의 지원을 받고 있고, 별도의 API key가 없어도 접근이 가능해 유용한 데이터들을 얻을 수 있습니다. 

https://clustermapping.us/

 

Home page | U.S. Cluster Mapping

The U.S. Cluster Mapping site provides over 50 million open data records on industry clusters and regional business environments in the U.S. to promote economic growth and national competitiveness. It is led by Harvard Business Schools Institute for Strate

clustermapping.us

2) API 가져오는 방법 메뉴얼

https://clustermapping.us/about/clustermappingus-data-api-documentationhttps://clustermapping.us/

 

Home page | U.S. Cluster Mapping

The U.S. Cluster Mapping site provides over 50 million open data records on industry clusters and regional business environments in the U.S. to promote economic growth and national competitiveness. It is led by Harvard Business Schools Institute for Strate

clustermapping.us

[1] Python 으로 가지고 올 수 있는 데이터 대분류 확인하기

base='http://clustermapping.us/data/'
response = requests.get(base)
data = response.json()
data
{out} {'meta': 'http://clustermapping.us:4001/data/meta',
 'region': 'http://clustermapping.us:4001/data/region',
 'cluster': 'http://clustermapping.us:4001/data/cluster'}

메타(Meta): 어떤 변수가 있는지에 대한 데이터

지역(Region): 어느 지역이 있는지에 대한 데이터

클러스터(Cluster): 클러스터 정보 데이터

 

[2] Python 으로 가지고 올 수 있는 데이터 대분류(상세) 확인하기

dict 을 이용하면 상세 사이트 asset 이 나오는데, 이런 형태의 리스트 구조를 list of dictionaries 라고 한다

#메타데이터 가져오기
#Retrieves entire list of keys, labels, formats, and other data used for indicators throughout the site
base='http://clustermapping.us/data/meta/dict'
response = requests.get(base)
data = response.json()              
mdict=data
mdict
{out}
{'varTypes': [{'label': 'Performance', 'key': 'performance'},
  {'label': 'Business Environment', 'key': 'business'},
  {'label': 'Demographics & Geography', 'key': 'structure'}],
 'vars': [{'label': 'Specialization',
   'key': 'specialization_tl',
   'mapTypes': ['cluster'],
   'colors': {'max': '#0074ea',
    'middle': '#76dddd',
    'zero': '#76dddd',
    'min': '#fffddc',
    'palette': ['#fffddc',
     '#fffddc',
     '#76dddd',
     '#76dddd',
     '#76dddd',
     '#76dddd',
     '#76dddd',
     '#76dddd',
     '#0074ea',
     '#0074ea']},
   'format': ',f',
   'calc': True,
   'calc_type': 'specialization',
   'calc_source': ['cluster_emp_per_tf', 'lq_tf', 'emp_tl', 'est_tl']},
  {'label': 'Employment',
   'subtitle': 'Private, Non-Agricultural Employment',
   'key': 'emp_tl',
   'mapTypes': ['cluster'],
   'colors': {'max': '#1e2a79',
    'middle': '#0074ea',
    'zero': '#4dc5ff',
    'min': '#dcfbff',
    'palette': ['#dcfbff',
     '#a5eeff',
     '#86ddff',
     '#4dc5ff',
     '#33aaff',
     '#328af8',
     '#0074ea',
     '#1155cc',
     '#003f9e',
     '#1e2a79']},
   'format': ',f',
   'type': 'cagr',
   'plot_type': 'cagr',
   'plot_scale': 'log',
   'spark_type': '1-rank',
   'lookup': 'jobs',
   'avg': True},

이 형태를 dataframe으로 바꿔

mdict = pd.DataFrame.from_dict(mdict, orient='index')
mdict

주려면 아래의 코드를 써주면 된다. 그럼 아래와 같이 dataframe 형태로 변환되어 나온다. 

[3] Meta:Regions 정보 가져오기

#메타데이터 가져오기
#Retrieves valid region types
base='http://clustermapping.us/data/meta/regions'
response = requests.get(base)
data = response.json()
data
regions=data
regions

[3] Meta: Country 정보 가져오기

#메타데이터 가져오기
#Retrieves basic data, including geographic information for all the regions of a given type, e.g., [country|state|economic|msa].
#:type parameter can be:
#● Country = “country”
#● State = “state”
#● Economic Area = #“economic”
#● Micro/Macro Statistical Area = “msa”
base='http://clustermapping.us/data/meta/regions/country'
response = requests.get(base)
data = response.json()
data
country=data
country

country = pd.DataFrame(country)
country

[4] Meta: Conclusion 

나머지는 전부 동일하다. 

base='http://clustermapping.us/data/meta/regions/[찾고자하는 Dataset]'
response = requests.get(base)
data = response.json()
data
[Dataset]=data
[Dataset]
[Dataset]= pd.DataFrame([Dataset])
[Dataset]

에서 [Dataset] 부분에 원하는 값을 넣어주기만 하면 끝이다. 

반응형

댓글