mercredi 23 juin 2021

How to use conditional if/else for multiple dataframe python

I've been been trying the lat and long of dataframe using geocoder. As i have a huge data and to make the performance better, I use this pseudocode for my code:

Pseudocode:

  • Geomap find the lat/long based on distict city and save it into a dict/file.
  • From the dict/file, map the lat/long based on distict city.

I managed to get the lat/long of distict city and save it into a file. However, I got an error when try to map the data to other data of city as Im not sure how to do so.

My code:

import pandas as pd
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
geolocator = Nominatim(user_agent="my_request")
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)

data = {'CITY': ['SCOTLAND', 'SCOTLAND', 'SCOTLAND', 'MANCHESTER', 'SCOTLAND', 'MANCHESTER', 'SCOTLAND', 'MANCHESTER']}

deduplokasi = data.drop_duplicates('CITY')
deduplokasi['LOCATION'] = deduplokasi['CITY'].apply(geocode)
deduplokasi['LATITUDE'] = deduplokasi['LOCATION'].apply(lambda x: x.latitude if x else None)
deduplokasi['LONGITUDE'] = deduplokasi['LOCATION'].apply(lambda x: x.longitude if x else None)

#save data as dictionary/file
deduplokasi.to_excel  (r'dir')
data2 = pd.read_excel(r'dir')

def func(a):
    if a in data2['CITY']:
        return data2['LATITUDE']
    else:
        return "0"

data["LATITUDE"] = data["CITY"].apply(lambda x: func(x))

Error: All data become 0

enter image description here

Aucun commentaire:

Enregistrer un commentaire