2021-02-27

Applying .get() function On a Pandas series

I am working on sample dataset to retrieve location information from address(some details are changed for identification purpose);

temp2=pd.DataFrame({'USER_ID':[1268,12345,4204,4208], 'IP_ADDR':['142.176.00.83','24.000.63.230','187.178.252.99','187.178.250.99']})

My goal is to get Lattitude and longitude information using the ip2geotools python package. The syntax is follows;

!pip install ip2geotools
response = DbIpCity.get(a, api_key='free')
json_file = response.to_json()

where a='142.176.00.83'. Then we get a JSON file like this;

'{"ip_address": "142.176.00.83", "city": "Charlotte", "region": "Prince Edward", "country": "CA", "latitude": 46.2, "longitude": -63.131}'

I am trying to apply the function on an entire pandas series (vectored form) and retrieve latitude and longitude as two different columns. Here is my attempt:

temp2['y'] = temp2['IP_ADDR'].apply(lambda x: DbIpCity.get(x, api_key='free'))

But it seems it doesn't like this syntax, InvalidRequestError: . Can I kindly get some help on how to vectorize this operation and retrieve fields from JSON file. thanks



from Recent Questions - Stack Overflow https://ift.tt/2MuYicn
https://ift.tt/eA8V8J

No comments:

Post a Comment