【发布时间】:2020-03-26 05:28:37
【问题描述】:
我有一列带有邮政编码或 NaN 的 pandas 数据框。我想创建一个 state_abbreviation 列来制作 cloropleth 地图。
_id 邮编
0 NaN
1 12345
dtype: 对象
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()
谢谢。
【问题讨论】:
-
print (type(zipcode))是什么? -
print(type(zipcode)) <class 'pandas.core.series.Series'>。我尝试更改为`print(type(zipcode)) <class 'pandas.core.frame.DataFrame'>,并收到相同的错误AttributeError: 'NoneType' object has no attribute 'upper'