简体   繁体   中英

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. I'm then running the IP addresses through the ipstack.com API and getting lat and long. I'm then trying to push the lat and long to the opencage API but here is where I am running into issue. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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