我正在处理Alienvault的声誉.data文件。它是40k恶意IP地址及其位置的列表。我已经像这样阅读了文件
addresses_columns = ["IP", "Reliability", "Risk", "Type", "Country", "Locale", "Coords", "x"]
ip_addresses = pd.read_csv('reputation.data', sep='#', names=addresses_columns)
我想取出Coords列并使用lat long数字将它们绘制成世界地图上的散点图。坐标是纬度和经度,在列中以逗号分隔,它们是浮点数,如21.0333003998,105.849998474。世界地图从Basemap编码,因此
#import the world map from basemap
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
# Define the projection, scale, the corners of the map, and the resolution.
m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80,\
llcrnrlon=-180,urcrnrlon=180,lat_ts=20,resolution='c')
# Draw the coastlines
m.drawcoastlines()
# Color the continents
m.fillcontinents(color='#ffcc99',lake_color='#ccffff')
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,91.,30.))
m.drawmeridians(np.arange(-180.,181.,60.))
# fill in the oceans
m.drawmapboundary(fill_color='#ccffff')
plt.title("Map of IP Addresses")
plt.show
所以现在我想把长长的数字绘制到地图上。这就是我所拥有的。
coordinates = ip_addresses[['Coords']]
for index in range(len(coordinates)):
lat, lon = coordinates[index].split(",")
print "lat=%s, lon=%s" % (lat, lon)
x,y = map(lon, lat)
map.plot(x, y, 'bo', markersize=2)
这是输出
Traceback (most recent call last): File "./assignment.py", line 85, in <module>
lat, lon = coordinates[index].split(",") File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2059, in __getitem__
return self._getitem_column(key) File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2066, in _getitem_column
return self._get_item_cache(key) File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1386, in _get_item_cache
values = self._data.get(item) File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3543, in get
loc = self.items.get_loc(item) File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 2136, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas/index.c:4145)
File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4009)
File "pandas/src/hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13166)
File "pandas/src/hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:13120)
KeyError: 0
为什么没有散点图?任何帮助表示赞赏。
答案 0 :(得分:0)
使用以下示例可以重现错误。
import pandas as pd
import numpy as np
x = np.random.rand(10, 2)
d = ["{},{}".format(x[i,0], x[i,1]) for i in range(x.shape[0])]
df = pd.DataFrame({"Coords": d})
coordinates = df[['Coords']]
for index in range(len(coordinates)):
lat, lon = coordinates[index].split(",")
print "lat=%s, lon=%s" % (lat, lon)
问题是行coordinates = df[['Coords']]
,其中使用单个元素列表尝试列索引。这是不可能的。
而是使用
coordinates = df['Coords']