为什么我的程序不接受Pandas数据系列作为输入?

时间:2018-10-23 22:39:46

标签: python api

为此工作开展学校项目。我基本上是从Wikipedia历史中删除IP地址。然后,我通过ipstack.com API运行IP地址,并且越来越长。然后,我尝试将lat和long推入opencage API,但这是我遇到的问题。如果我对一个经纬度进行硬编码并长时间输入,则会返回一个城市。

result = geocoder.opencage([latitude, longitude], key=key, method='reverse')
print(result.city)

但是当我尝试遍历经纬度较长的列表时,我得到了一个错误

TypeError: cannot convert the series to <class 'float'>

我想这可能与系列类型有关,但是我可能再一次完全错了。有什么想法吗?

from bs4 import BeautifulSoup
import requests
from urllib.request import urlopen
import pandas as pd
import re
from opencage.geocoder import OpenCageGeocode
import geocoder

response = requests.get("https://en.wikipedia.org/w/index.php?title=Gun_laws_in_New_Hampshire&action=history")

soup = BeautifulSoup(response.text, "lxml")

bdi_text = []

for bdi_tag in soup.find_all('bdi'):
    bdi_text.append(bdi_tag.text)


ip_addresses = []


for element in bdi_text:
    ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', element)
    if len(ip) > 0:
        ip_addresses.append(ip)


api_key = '?access_key={YOUR_API_ACCESS_KEY}'

resolved_ips = []

for ips in ip_addresses:
    api_call = requests.get('http://api.ipstack.com/' + ips[0] + api_key).json()
    resolved_ips.append(api_call)

ip_df = pd.DataFrame.from_records(resolved_ips)
ip_df = ip_df[['city','country_code','latitude','longitude']]


key = 'my_API_key'

latitude = ip_df['latitude']
longitude = ip_df['longitude']

result = []
print(len(latitude))
for latlong in range(0,len(latitude)):
    result = geocoder.opencage([latitude, longitude], key=key, method='reverse')
    print(result.city)

2 个答案:

答案 0 :(得分:1)

您的实施过程很艰难。我会做这样的事情

def make_city(row):
    result = geocoder.opencage(float(row['latitude']), #lat of target
                               float(row['longitude']), #long of target
                               key=key, #API key that I will keep to myself
                               method='reverse')
    print(result.city)

ip_df.apply(make_city, axis = 1)

答案 1 :(得分:0)

我认为它与您传递的类型混淆:

不确定数据的确切结构,请尝试以下操作:

latitude = ip_df['latitude'].astype(float)
longitude = ip_df['longitude'].astype(float)