 Welcome back. I'm Dan. I'm Nathan. Today we're going to be showing you how to get latlongs using the Google Maps API for all the cities in that data frame that we've been building over the last few videos. Let's go exploring like Lewis and Clark back there. Alright so the first thing we're going to do is we've got this data frame. We're going to just load load it from that file we saved in a previous video. In a previous video we showed you how to take a panda's data frame and save it to a file and here we're just going to be loading it up and we're going to be calling it DF. So to get a list of all the cities, all the unique cities in this data frame of of Craigslist data, one way to do that is to specify the column that we want to query and then we'll do dot unique and put in parentheses and that'll spit out an array of all our cities of all the cities and we'll just all I'll call that city list and set that equal to to that. So that's our variable there. Now we're going to have to do a for loop to iterate over all the cities in this list that we just created and inside this for loop we're going to have a fair number of things to do so we'll just start off with the outline of our for loop with for place in city list so taking it city by city we're just going to go the first part is to take care of all of our cities some of them are multiple cities with a slash in the middle of them to separate the cities but we don't necessarily care about the dead center between the two cities so we're just going to take the location of the first city in those combinations. Yeah what happens sometimes when we're querying the Google API as we were testing this beforehand we found that it somehow sometimes had some issues with when it had a slash in between two cities and so what we're going to do here is just do a just do a temporary fix just to get a sort of a close result we'll just use a string method split to split along that that slash and then we'll just take the first city and just take the first city doing the first index so that's that I'll get us the first city if there's multiple cities but if there's not then it'll just give us a full full length as is the next step here that we came across is there are a couple locations that didn't quite work out by sending it to Google one of those examples is the Florida Keys it couldn't quite the geocoding API couldn't quite determine what the Florida Keys were so we're going to put in a try accept here and on the accept part we're going to actually set the latitude long to not a number but for the try part we're going to deal with actually getting the results for all the other cities basically so if you haven't run across try and accept they're pretty simple we will do a video about them in the future but basically it's a way of handling Python exceptions so you'll try this bit of code and if you run into a certain exception or error do this other thing and so what we're going to be doing here is just geocoding we'll call this geo from our gmaps object this was done in a previous video so if this doesn't fully make sense feel free to jump back and watch that video so I'm just typing this out lat long equals geo code and we're gonna be taking geometry these are just tags from the XML and don't forget that zero line above thank you all right and if we run into an index error we want to take the following action that index error error is from that zero that I just mentioned basically when Google can't find a result for us it sends us back an empty list and an empty list of course does not have a zero index and so we're just going to pass in a latitude and longitude of none so that later on it doesn't give us values that are incorrect or anything we can clearly correct those later down the road just by searching for the latitudes with none in this step we're going to be doing a little bit more work with the data frame so so now that we got the latitude and longitude we want to actually store it into cities that we are associating it with so we're going to be for for this data frame we're going to be locating every we're going to be locating every row where the column city is equal to the place name from the for loop up here and then in a column called lat we will be storing the the latitude value that we just pulled thank you and we haven't previously created a lat and long longitude column yet but pandas will actually just do this on the fly and create a new column as it needs to all right so I think actually we're where we need to be in our for loop so we'll just give this a run this will take a little bit of time because we do have about 413 cities but after we get back we'll want to jump ahead and we while we're while we're waiting for this to go we do just want to mention that if you do this enough you will run it run up against some of the API limits that Google sets so for instance for the Google Maps API for the free free tier of their service I think the limits at 2,500 or so requests so we're going to be okay with these 400 or so but as soon as you start doing a ton of these you're going to run into some some issues there that's one reason why we wanted to pull out the city unique the unique cities initially we didn't want to go through line by line and do all 8,000 rows that we have because I just wouldn't quite work in a day shoot it's okay oh so we did get a little bit of an attribute error it's okay the laugh for whatever reason the last item in our list of cities is not a number it's not a string so it just had an error but for the other cities in them for the actual cities in our data frame we we now have some information so you can see Jackson Mississippi now we have a lot long associated with with with that city so we can see multiple cities just quickly here we do a DF dot group by and then we want group and by city and then we'll do dot head and just do one of each we'll give us one from each city and sure enough we have just to verify it we do have different latitude and longitudes for each city so well good I hope you enjoyed this video we're excited to get moved forward with putting these putting these places on a map and and and going that route