简体   繁体   English

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

[英]Why won't my program accept a Pandas data series as input?

Working on this for a school project. 在学校项目中为此工作。 I'm basically scraping IP addresses off of the wikipedia history. 我基本上是从Wikipedia历史中删除IP地址。 I'm then running the IP addresses through the ipstack.com API and getting lat and long. 然后,我通过ipstack.com API运行IP地址,并且越来越长。 I'm then trying to push the lat and long to the opencage API but here is where I am running into issue. 然后,我尝试将lat和long推入opencage API,但这是我遇到的问题。 If I hard code a lat and long into this it returns a city. 如果我对一个经纬度进行硬编码并长时间输入,则会返回一个城市。

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

But when I try to loop through a lat and long list I get an error 但是,当我尝试遍历经纬度较长的列表时,出现错误

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

I'm thinking this might have to do with series type but then again I might be going about it completely wrong. 我想这可能与系列类型有关,但是我可能再一次完全错了。 Any ideas? 有任何想法吗?

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)

Your implementation is rough. 您的实施过程很艰难。 I would do something like this 我会做这样的事情

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)

I think its getting confused with the type you are passing: 我认为它与您传递的类型混淆:

Not sure of the exact structure of your data, but try this instead: 不确定数据的确切结构,请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM