跳转到主要内容

将县FIPS添加到表格数据中

项目描述

AddFIPS

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 是一个与 3.7+ 版本的 Python 兼容的 Python 包。

如果您之前使用过 Python 包

pip install addfips
# or
pip install --user addfips

如果您之前没有使用过 Python 包,请获取 pip,然后返回。

功能

  • 使用州的全称或邮政缩写
  • 与所有州、领地和哥伦比亚特区兼容
  • 略微模糊匹配允许缺少重音符号和不同的名称格式(例如 "Nye County" 或 "Nye', "Saint Louis" 或 "St. Louis", "Prince George's" 或 "Prince Georges")
  • 包括最新的 2015 地理数据(向阿拉斯加库斯利瓦克人口普查区,阿拉斯加和南达科他州奥格拉拉拉科塔县致意)

请注意,一些州有同名县和相当于县的独立市(例如马里兰州巴尔的摩市及县,弗吉尼亚州里士满市及县)。如果仅传递名称(例如 "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 从 stdin 读取。
  • --delimiter:字段分隔符,默认为逗号','。
  • --state-field:包含州名的字段名
  • --state-name:用于所有行的名称、邮政缩写或州FIPS代码。
  • --county-field:包含县名的字段名。如果此字段为空,输出将包含两位数的州FIPS代码。
  • --vintage:使用较早的县名和FIPS代码。例如,弗吉尼亚州克利夫顿福德市不包括在2010年或更晚的年份中。
  • --no-header:表示输入文件没有标题。 --state-field--county-field 作为字段索引解析。
  • --err-unmatched:无法匹配的行将打印到 stderr,而不是 stdout

输出是一个带有附加到前面的新列 "fips" 的CSV文件。当 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

列号是从1开始的。

为特定州的县使用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

可以在Python脚本中使用AddFIPS

>>> 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'}

由于FIPS代码可能包含前导零,因此AddFIPS.get_state_fipsAddFIPS.get_county_fips 的结果都是字符串。

AddFIPS(vintage=None)

AddFIPS类接受一个关键字参数 vintage,可以是 200020102015。任何其他值都将使用最新的年份。未来可能会添加其他年份。

get_state_fips(self, state) 根据州名或邮政编码返回两位数FIPS代码。

get_county_fips(self, county, state) 根据县名和州名/缩写/FIPS返回五位数FIPS代码。

add_state_fips(self, row, state_field='state') 返回带有两位州FIPS代码的输入行。输入行可以是 dictlist。如果是 dict,则添加 'fips' 键。如果是 list,则将FIPS代码添加到列表的开始位置。

add_county_fips(self, row, county_field='county', state_field='state', state=None) 返回带有五位县FIPS代码的输入行。输入行可以是 dictlist。如果是 dict,则添加 'fips' 键。如果是 list,则将FIPS代码添加到列表的开始位置。

许可证

在GNU通用公共许可证版本3下分发。有关更多信息,请参阅LICENSE。

项目详情


下载文件

下载适合您平台的自定义文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

addfips-0.4.2.tar.gz (134.7 kB 查看散列)

上传

构建分布

addfips-0.4.2-py3-none-any.whl (120.4 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面