【问题标题】:Creating State Abbreviations from a Zip Code column that contains NaNs in Pandas从 Pandas 中包含 NaN 的邮政编码列创建州缩写
【发布时间】: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'

标签: python pandas


【解决方案1】:

我认为您需要指定列。

你可能想试试这个。

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

【讨论】:

  • 感谢您的建议。我在申请时收到同样的错误。 AttributeError: 'NoneType' object has no attribute 'upper'
【解决方案2】:

我认为我的方法存在根本缺陷。此处提供了一种更快的方法来创建减少 sql 查询数量的状态列(和城市!):TypeError: 'Zipcode' object is not subscriptable。谢谢大家的提问和建议。

【讨论】:

    猜你喜欢
    • 2015-05-07
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2023-02-21
    • 2015-09-25
    • 1970-01-01
    • 2021-04-13
    • 2016-05-16
    相关资源
    最近更新 更多