计算值时在Dataframe中出现KeyError'City'吗?

时间:2018-12-05 16:34:47

标签: python pandas dataframe geolocation geopy

在过去的几天里,我在使用Python中的数据框时遇到了一些麻烦-我一直在尝试计算数据框中的“城市”列的坐标值(但是,该列有约10500行尝试在该列上运行任何功能时,我总是会收到一个KeyError:“ City”。

背景

我一直在输入一个.csv文件,该文件包含约10500行x 15列,并将其转换为数据帧。然后,我在末尾添加了一个称为“坐标”的附加列,以保存“城市”列的坐标值。

#inserting my .csv file to convert to a dataframe
df = pd.read_csv("/path/to/test.csv")

#creating new column 'Coordinates' to insert into dataframe at the end
df['Coordinates'] = '0,0'

# practice location finding using geopy
geolocator = Nominatim(timeout =10)

#method to calculate latitude and longitude
def eval_results(x):
try:
    return (x.latitude, x.longitude)
except:
    return (None, None)

#calculating the coordinates value by running the following methods on the 'City' column
df['Coordinates'] = df['City'].apply(geolocator.geocode,
timeout=1000000).apply(lambda x: eval_results(x))

错误

但是,当我运行代码时,出现以下错误:

Traceback (most recent call last):
  File "metadata-geo.py", line 27, in <module>
metadata_df['Coordinates'] = metadata_df['City'].apply(geolocator.geocode,timeout=1000000).apply(lambda x: eval_results(x))
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2688, in __getitem__
return self._getitem_column(key)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 4115, in get
loc = self.items.get_loc(item)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'City'

我不太确定错误是什么,并且我已经浏览了很多有关KeyError的帖子,但是似乎没有一个适合我的情况。

2 个答案:

答案 0 :(得分:0)

这意味着“城市”列不存在

这可能是“城市”吗?

否则,您可以通过输入来检查列名:

print df.columns

(我看到python 2.7)

print(df.columns)

适用于python 3 +

然后选择所需的列

答案 1 :(得分:0)

我发现了我的错误,我在打开文件时没有分隔列,因此在读取CSV文件时缺少sep='\t'