将县FIP添加到表格数据中
addfips的Python项目详细描述
addfips是一个将州或县fips代码添加到 只包含这些地理位置的名称。
FIPS代码是美国各地的官方身份证号码。他们是 对于匹配来自不同来源的数据非常有用。
假设您有这样的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包,与python 2.7、python 3和 皮比。它在python的标准库之外没有依赖项。
如果您以前使用过python包:
pip install addfips # or pip install --user addfips
如果您以前没有使用过python包,get pip,那么来吧 回来。
您还可以克隆repo并使用 python setup.py install。
功能
- 对州使用全名或邮政缩写
- 与所有州、地区和哥伦比亚特区合作
- 稍微模糊的匹配允许丢失的双十字标记和 不同的名称格式(“NYE County”或“NYE”,“Saint Louis”或“St. “路易”,“乔治王子”或“乔治王子”)
- 包括最新的2015年地理位置(呼喊到Kusilvak人口普查 阿拉巴马州阿拉科塔地区和奥格拉拉拉科塔公司(SD)
注意,有些州有县和相当于县的独立 同名城市(例如巴尔的摩市和县、马里兰州、里士满 弗吉尼亚州市和县)。如果 只传递了名字(“巴尔的摩”)。
命令行工具
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:行 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
列号是一个索引。
为特定州的县添加FIP。它们相当于:
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代码比使用县名(可能是常用词)更安全 (例如厨师):
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脚本:
>>>importaddfips>>>af=addfips.AddFIPS()>>>af.get_state_fips('Puerto Rico')'72'>>>af.get_county_fips('Nye',state_name='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(vintage=none)
addfips类接受一个关键字参数vintage,它可以是 可以是2000、2010或2015。任何其他值都将使用 最近的年份。其他年份的葡萄酒可能会在未来添加。
get_state_fips(self,state)返回基于 州名或邮政编码。
get_county_fips(self,county,state)返回五位fips代码 基于县名和州名/缩写/FIPS。
add_state_fips(self,row,state_field='state')返回inpuT型 添加了两个figit state fips代码的行。输入行可以是 dict或list。如果是dict,则添加“fips”键。如果 list,fips代码添加在列表的开头。
add_county_fips(self,row,county_field='county', state_field='state',state=none)返回带有 增加了5个菲吉特县的菲普斯密码。输入行可以是dict或 一个list。如果是dict,则添加“fips”键。如果是list,则 fips代码添加在列表的开头。
许可证
根据GNU通用公共许可证第3版发布。见许可证 更多信息。