将县 FIPS 添加到表格数据
项目描述
添加FIPS
AddFIPS 是一种将州或县 FIPS 代码添加到仅包含这些地理名称的文件的工具。
FIPS 代码是美国地方的官方 ID 号码。它们对于匹配来自不同来源的数据非常宝贵。
假设您有一个这样的 CSV 文件:
state,county,statistic
IL,Cook,123
California,Los Angeles County,321
New York,Kings,137
LA,Orleans,99
Alaska,Kusilvak,12
AddFIPS 允许您这样做:
> addfips --county-field=county input.csv
countyfp,state,county,statistic
17031,IL,Cook,123
06037,California,Los Angeles County,321
36047,New York,Kings,137
22071,LA,Orleans,99
02270,Alaska,Kusilvak,12
安装
AddFIPS 是一个与 Python 3 兼容的 Python 包。
如果您以前使用过 Python 包:
pip install addfips
# or
pip install --user addfips
如果您以前没有使用过 Python 包,请获取 pip,然后再回来。
您还可以克隆存储库并使用python setup.py install
.
特征
- 使用州的全名或邮政缩写
- 与所有州、领地和哥伦比亚特区合作
- 稍微模糊的匹配允许丢失变音符号和不同的名称格式(“Nye County”或“Nye”、“Saint Louis”或“St. Louis”、“Prince George's”或“Prince Georges”)
- 包括最新的 2015 年地理区域(请关注 AK 的 Kusilvak 人口普查区和 SD 的 Oglala Lakota Co.)
请注意,某些州拥有同名的县和与县相当的独立城市(例如,巴尔的摩市和县,马里兰州,里士满市和县,弗吉尼亚州)。如果仅传递名称(“Baltimore”),AddFIPS 的行为可能会选择错误的地理位置。
命令行工具
usage: addfips [-h] [-V] [-d CHAR] (-s FIELD | -n NAME) [-c FIELD]
[-v VINTAGE] [--no-header]
[input]
AddFIPS codes to a CSV with state and/or county names
positional arguments:
input Input file. default: stdin
optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
-d CHAR, --delimiter CHAR
field delimiter. default: ,
-s FIELD, --state-field FIELD
Read state name or FIPS code from this field
-n NAME, --state-name NAME
Use this state for all rows
-c FIELD, --county-field FIELD
Read county name from this field. If blank, only state
FIPS code will be added
-v VINTAGE, --vintage VINTAGE
2000, 2010, or 2015. default: 2015
--no-header Input has no header now, interpret fields as integers
-u, --err-unmatched Print rows that addfips cannot match to stderr
选项和标志:
input
:(位置参数)文件的名称。如果为空,addfips
则从标准输入读取。--delimiter
: 字段分隔符,默认为 ','。--state-field
: 包含州名的字段名--state-name
:用于所有行的姓名、邮政缩写或州 FIPS 代码。--county-field
:包含县名的字段的名称。如果这是空白,则输出将包含两个字符的状态 FIPS 代码。--vintage
:使用较早的县名和 FIPS 代码。例如,弗吉尼亚州克利夫顿福吉市不包括在 2010 年或以后的年份。--no-header
: 表示输入文件没有头文件。--state-field
并被--county-field
解析为字段索引。--err-unmatched
:addfips
无法匹配的行将打印到 stderr,而不是 stdout
输出是一个 CSV,前面附加了一个新列“fips”。当addfips
无法匹配时,fips 列将有一个空值。
例子
添加状态 FIPS 代码:
addfips data.csv --state-field fieldName > data_with_fips.csv
添加州和县 FIPS 代码:
addfips data.csv --state-field fieldName --county-field countyName > data_with_fips.csv
对于没有标题行的文件,使用数字来指代具有州和/或县名称的列:
addfips --no-header-row --state-field 1 --county-field 2 data_no_header.csv > data_no_header_fips.csv
列号是单索引的。
来自特定州的县的 AddFIPS。这些是等价的:
addfips ny_data.csv -c county --state-name NY > ny_data_fips.csv
addfips ny_data.csv -c county --state-name 'New York' > ny_data_fips.csv
addfips ny_data.csv -c county --state-name 36 > ny_data_fips.csv
使用备用分隔符:
addfips -d'|' -s state pipe_delimited.dsv > result.csv
addfips -d';' -s state semicolon_delimited.dsv > result.csv
将不匹配的行打印到另一个文件:
addfips --err-unmatched -s state state_data.csv > state_data_fips.csv 2> state_unmatched.csv
addfips -u -s STATE -c COUNTY county_data.csv > county_data_fips.csv 2> county_unmatched.csv
来自其他程序的管道:
curl http://example.com/data.csv | addfips -s stateFieldName -c countyField > data_with_fips.csv
csvkit -c state,county,important huge_file.csv | addfips -s state -c county > small_file.csv
管道到其他程序。在包含大量文本的文件中,使用 FIPS 代码进行过滤比使用可能是常用词(例如 cook)的县名更安全:
addfips culinary_data.csv -s stateFieldName -c countyField | grep -e "^17031" > culinary_data_cook_county.csv
addfips -s StateName -c CountyName data.csv | csvsort -c fips > sorted_by_fips.csv
API
AddFIPS 可用于您的 Python 脚本:
>>> import addfips
>>> af = addfips.AddFIPS()
>>> af.get_state_fips('Puerto Rico')
'72'
>>> af.get_county_fips('Nye', state='Nevada')
'32023'
>>> row = {'county': 'Cook County', 'state': 'IL'}
>>> af.add_county_fips(row, county_field="county", state_field="state")
{'county': 'Cook County', 'state': 'IL', 'fips': '17031'}
AddFIPS.get_state_fips
和的结果AddFIPS.get_county_fips
是字符串,因为 FIPS 代码可能有前导零。
课程
AddFIPS(复古=无)
AddFIPS 类采用一个关键字参数 ,vintage
可以是2000
,2010
或2015
。任何其他值将使用最近的年份。未来可能会添加其他年份。
get_state_fips(self, state) 根据州名或邮政编码返回两位数的 FIPS 代码。
get_county_fips(self, County, state) 根据县名和州名/缩写/FIPS 返回五位数的 FIPS 代码。
add_state_fips(self, row, state_field='state')
返回添加了两个数字状态 FIPS 代码的输入行。输入行可以是 adict
或 a list
。如果 a dict
,则添加 'fips' 键。如果 a list
,则将 FIPS 代码添加到列表的开头。
add_county_fips(self, row, County_field='county', state_field='state', state=None)
返回添加了五位数县 FIPS 代码的输入行。输入行可以是 adict
或 a list
。如果 a dict
,则添加 'fips' 键。如果 a list
,则将 FIPS 代码添加到列表的开头。
执照
根据 GNU 通用公共许可证第 3 版分发。有关更多信息,请参阅许可证。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。