首页 > 解决方案 > 从 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()

谢谢你。

标签: pythonpandas

解决方案


我认为您需要指定列。

你可能想试试这个。

state[state['zip'].notnull()]

推荐阅读