我们的数据并非没有争议无需改革——用户须要确保 geoJSON 键与熊猫数据框架之间具有1:1的映射。下面就是之前实例所需的简明的数据框架映射:我们的国度信息是一个列有FIPS 码、国度名称、以及经济信息(列名省略)的 CSV 文件:
- 00000,US,United States,154505871,140674478,13831393,9,50502,100
- 01000,AL,Alabama,2190519,1993977,196542,9,41427,100
- 01001,AL,Autauga County,25930,23854,2076,8,48863,117.9
- 01003,AL,Baldwin County,85407,78491,6916,8.1,50144,121
- 01005,AL,Barbour County,9761,8651,1110,11.4,30117,72.7
在 geoJSON 中,我们的国度外形是以 FIPS 码为id 的(感激 fork 自 Trifacta 的相干信息)。为了简便,实际外形已经做了简单,在示例数据可以找到完全的数据集:
我们须要匹配 FIPS 码,确保匹拍┞俘确,不然 Vega 无法精确的紧缩数据:
- import json
- import pandas as pd
- #Map the county codes we have in our geometry to those in the
- #county_data file, which contains additional rows we don't need
- with open(county_geo, 'r') as f:
- get_id = json.load(f)
- #Grab the FIPS codes and load them into a dataframe
- county_codes = [x['id'] for x in get_id['features']]
- county_df = pd.DataFrame({'FIPS_Code': county_codes}, dtype=str)
- #Read into Dataframe, cast to string for consistency
- df = pd.read_csv(county_data, na_values=[' '])
- df['FIPS_Code'] = df['FIPS_Code'].astype(str)
- #Perform an inner join, pad NA's with data from nearest county
- merged = pd.merge(df, county_df, on='FIPS_Code', how='inner')
- merged = merged.fillna(method='pad')
- >>>merged.head()
- FIPS_Code State Area_name Civilian_labor_force_2011 Employed_2011 \
- 0 1001 AL Autauga County 25930 23854
推荐阅读
大年夜第一天起,刘强东(微博)就定下了一个准则,只和有名品牌合作。 刘强东称做自营最看重质量,并且自营体系须要很长时光构建,然后向第三方卖家开放,这也是>>>详细阅读
本文标题:10行Python代码创建可视化地图
地址:http://www.17bianji.com/lsqh/37136.html
1/2 1