2017-03-16 83 views
1

我正在Alienvault的reputation.data文件上工作。它是一個40k惡意IP地址及其位置的列表。我在像這樣在matplotlib底圖中繪製逗號分隔座標(lat,long)的Python

addresses_columns = ["IP", "Reliability", "Risk", "Type", "Country", "Locale", "Coords", "x"] 
ip_addresses = pd.read_csv('reputation.data', sep='#', names=addresses_columns) 

我想拿出COORDS列,並使用LAT長的數字來繪製它們作爲一個世界地圖上的散點圖讀取文件。座標是緯度和經度,逗號分隔在列中,它們是像21.0333003998,105.849998474這樣的浮點數。世界地圖由底圖編碼從而

#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 

所以現在我想將Lat Long網圖繪出到地圖上。這是我的。

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

歡迎#1。 「沒有快樂」不是一個足夠的問題描述。問題是什麼?如果你運行你的代碼會發生什麼?如果出現錯誤,請包含回溯。相應地編輯你的問題。 – ImportanceOfBeingErnest

回答

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']