Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dict and Isin
#1
I need help on data dict and isin codes. Can anybody assist what is the python codes to achieve the results. Thanks.

I have two dataframes.

Dataframe1 (already convert into dict)
Address Driver
New York John
Los Angeles Tim

Dataframe 2
Address
47 W 13th St, New York, NY 10011, USA
1419 Westwood Blvd, Los Angeles | CA 90024-491

What would be the code to generate the following results?

Results
Address Driver
47 W 13th St, New York, NY 10011, USA John
1419 Westwood Blvd, Los Angeles | CA 90024-491 Tim
Reply
#2
What is Isin? ISIN as is in International Securities Identification Number or "Is this in that?"

You want to parse an address to find the city, find a driver for that city, and then append the driver's name to the address? Is that what you want to do?

An easy way is to search for all the keys in your driver database.
drivers = {
    "John":"New York",
    "Tim":"Los Angeles"
}

addresses = [
    "47 W 13th St, New York, NY 10011, USA",
    "1419 Westwood Blvd, Los Angeles | CA 90024-491",
    "1222 S 27th St, Lincoln, NE 68502, USA"
]

for address in addresses:
    for driver, city in drivers.items():
        if city in address:
            print(f"{address} {driver}")
            break
    else:
        print(f"{address} No Driver Available")
Output:
47 W 13th St, New York, NY 10011, USA John 1419 Westwood Blvd, Los Angeles | CA 90024-491 Tim 1222 S 27th St, Lincoln, NE 68502, USA No Driver Available
This becomes inefficient if you have lots of addresses and lots of drivers. If that is the case you should parse the address to extract the city and have a dictionary of cities (keys) and drivers (values).

Both of these fail if you have the same city name used for different cities like Portland Maine and Portland Oregon.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Regex Expression With Isin in Python eddywinch82 0 2,388 Apr-04-2021, 06:25 PM
Last Post: eddywinch82
  Sort a dict in dict cherry_cherry 4 101,775 Apr-08-2020, 12:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020