今天我第 100 万次拥有一个列出完整州名的数据集。但是,我需要它来列出州邮政编码缩写。这是我编写的代码片段,它使用来自通用网站的数据为我映射了更改。
1)有人知道或想到更好的解决方案吗?
2a)有人知道更好的网络参考吗?使用 USPS 网站(如下所示)似乎无法使用pd.read_html()
2b) 我也很难从pd.read_html()
以下 wiki 页面中分离出正确的表格:https ://en.wikipedia.org/wiki/List_of_U.S._state_abbreviations
import pandas as pd
# Make Generic Data For Demonstration Purpose
data = {'StName':['Wisconsin','Minnesota','Minnesota',
'Wisconsin','Florida','New York']}
df = pd.DataFrame(data)
# Get State Crosswalk From Generic Website
crosswalk = 'http://app02.clerk.org/menu/ccis/Help/CCIS%20Codes/state_codes.html'
states = pd.read_html(crosswalk)[0]
# Demo Crosswalking State Name to State Abbreviation
df['StAbbr'] = df['StName'].map(dict(zip(states['Description'],
states['Code'])))
# Demo Reverse Crosswalking Back to State Name
df['StNameAgain'] = df['StName'].map(dict(zip(states['Code'],
states['Description'])))