简体   繁体   中英

Check a dataframe column to see if a bool if True/False, if False, geocode only those values

I am using the [geocoder python API library][1]. I have a pandas dataframe column of boolean True/False based on if I already have that particular address geocoded or not. Is there a way to modify my existing code to geocode based on if I have it geocoded or not?

Right now all it does is print a True statement and then geocodes everything, regardless of the boolean I have. Help Please!

Here is another way to put it:

I have a dataframe of Tweets. If a Tweet was geocoded, I have marked that Tweet with a True (if it has been geocoded) or False (If it has not been geocoded). What I'm trying to do is check if the column is True, print out that row. Else if that row is False, then send it into my for loop to be geocoded. I will edit the original post for an input.

Here is my exisiting code:

for d in tweets2['Exist']:
    if d is True:
        print d
    elif d.any() is False:
        coord = []
        for index, row in tweets2.iterrows():
            print(row['location_x'])
            time.sleep(1.01)
            g = geocoder.osm(row['location_x'])
            geo = g.latlng
            print(geo)
            coord.append(geo)
    else:
        pass 

Here is an example of the JSON file as an input:

{
"data": [
    {
        "user_id": 3299796214, 
        "features": {
            "screen_name": "SaveOurSparrows", 
            "text": "Details confirmed for inquiry into #INEOS #Derbyshire #Fracking site! \n\nAnti Fracking, #keepitintheground #wesaidno\u2026", 
            "location": "West Pennine Moors AONB SSSI", 
            "tweets": 3, 
            "geo_type": "User location", 
            "primary_geo": "West Pennine Moors AONB SSSI", 
            "id": 3299796214, 
            "name": "SaveOurSparrows",
            "Exist": "True"
        }
    }, 
    {
        "user_id": 3302831409, 
        "features": {
            "screen_name": "ProjectLower", 
            "text": "Cutting down on energy costs is the dream for many #smallbusinesses, but to put ideas into practice isn\u2019t always ea\u2026", 
            "location": "Manchester", 
            "tweets": 1, 
            "geo_type": "User location", 
            "primary_geo": "Manchester", 
            "id": 3302831409, 
            "name": "Project Lower",
            "Exist": "False"
        }
    }, 
    {
        "user_id": 2205129714, 
        "features": {
            "screen_name": "AmbCanHaiti", 
            "text": "Petit-d\u00e9jeuner causerie le mercredi 28 mars 2018 \u00e0 l'h\u00f4tel Montana sur l'\u00e9nergie #micror\u00e9seaux #microgrids\u2026", 
            "location": "Haiti", 
            "tweets": 1, 
            "geo_type": "User location", 
            "primary_geo": "Haiti", 
            "id": 2205129714, 
            "name": "Canada en Ha\u00efti",
            "Exist": "False"
        }
    }
 ]

}

The simplest way is to walk over your data set, and if there is no coords key, add it:

for data in your_data_set['data']:
    data['coords'] = data.setdefault('coords',  geocoder.osm(data'location_x']).latlang)

Then, convert it into a dataframe.

If you already have it as a dataframe:

df.loc[df['coords'] == False, 'coords'] = geocoder.osm(df['location_x']).latlang

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