1

我正在使用 [geocoder python API 库][1]。根据我是否已经对该特定地址进行了地理编码,我有一个布尔值 True/False 的 pandas 数据框列。有没有办法根据我是否对其进行地理编码来修改我现有的代码以进行地理编码?

现在它所做的只是打印一个 True 语句,然后对所有内容进行地理编码,无论我拥有什么布尔值。请帮忙!

这是另一种说法:

我有一个推文数据框。如果一条推文被地理编码,我已经用真(如果它已经被地理编码)或假(如果它没有被地理编码)标记了这条推文。我要做的是检查该列是否为真,打印出该行。否则,如果该行为 False,则将其发送到我的 for 循环中进行地理编码。我将编辑原始帖子以供输入。

这是我现有的代码:

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 

以下是 JSON 文件作为输入的示例:

{
"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"
        }
    }
 ]

}

4

1 回答 1

0

最简单的方法是遍历你的数据集,如果没有coords键,添加它:

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

然后,将其转换为数据框。

如果您已经将其作为数据框:

df.loc[df['coords'] == False, 'coords'] = geocoder.osm(df['location_x']).latlang
于 2018-06-12T20:10:54.333 回答