python - 从 Pandas 中包含 NaN 的邮政编码列创建州缩写
问题描述
我有一列带有邮政编码或 NaN 的 pandas 数据框。我想创建一个 state_abbreviation 列以制作 cloropleth 地图。
_id 邮编
0 NaN
1 12345
数据类型:对象
from uszipcode import SearchEngine, SimpleZipcode, Zipcode
import pandas as pd
zipcode = "the sample series above"
def extract_state (zipcode_column):
"""
given a zipcode column, return a state abbr column
"""
search = SearchEngine()
state = zipcode_column
state[state.notna()]=state[state.notna()].apply(lambda x: search.by_zipcode(x).state_abbr)
return state
extract_state(zipcode)
我收到以下错误:
AttributeError: 'NoneType' object has no attribute 'upper'
我相信邮政编码搜索器无法处理 NaN,但我认为我在使用时没有使用 apply 功能选择它们
state.notna()
谢谢你。
解决方案
我认为您需要指定列。
你可能想试试这个。
state[state['zip'].notnull()]
推荐阅读
- android - 无法为抽象类“GoogleServicesTask”创建代理类。使用“com.google.gms:google-services:4.3.4”
- node.js - 在 npm 安装期间忽略本地 .npmrc 配置文件
- javascript - 在正确的输出上改变颜色
- reactjs - Material UI/React - 辅助文本未显示
- browser - 为什么 Cloudflare worker 上的站点预览与浏览器不同?
- r - 贝叶斯因子作为 ratioBF 的非逻辑输出
- arrays - 如何拆分多个数组,在laravel中插入不同的表
- .net - 工具“dotnet-try”安装失败
- javascript - 根据子元素的数量在图标之间切换
- javascript - 关于 React.useEffect() 和 React.useState() 的问题