Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查dataframe列,查看bool if True/False、if False是否仅对这些值进行地理编码_Python_Pandas_Boolean_Geocoder - Fatal编程技术网

Python 检查dataframe列,查看bool if True/False、if False是否仅对这些值进行地理编码

Python 检查dataframe列,查看bool if True/False、if False是否仅对这些值进行地理编码,python,pandas,boolean,geocoder,Python,Pandas,Boolean,Geocoder,我正在使用[geocoder python API库][1]。根据我是否已经对特定地址进行了地理编码,我有一个布尔值True/False的pandas dataframe列。有没有一种方法可以根据我是否进行了地理编码,将我现有的代码修改为地理编码 现在,它所做的就是打印一个True语句,然后对所有内容进行地理编码,而不管我使用的是什么布尔值。救命啊 下面是另一种说法: 我有一个tweet的数据帧。如果一条Tweet是地理编码的,我已经用True(如果它已经地理编码)或False(如果它没有地理编

我正在使用[geocoder python API库][1]。根据我是否已经对特定地址进行了地理编码,我有一个布尔值True/False的pandas dataframe列。有没有一种方法可以根据我是否进行了地理编码,将我现有的代码修改为地理编码

现在,它所做的就是打印一个True语句,然后对所有内容进行地理编码,而不管我使用的是什么布尔值。救命啊

下面是另一种说法:


我有一个tweet的数据帧。如果一条Tweet是地理编码的,我已经用True(如果它已经地理编码)或False(如果它没有地理编码)标记了该Tweet。我要做的是检查该列是否为真,打印出该行。否则,如果该行为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"
        }
    }
 ]

}最简单的方法是浏览数据集,如果没有
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

很难理解你在做什么。你有没有可能重新表述一下,或者添加一个输入/输出的例子?我有一个tweet的数据帧。如果一条Tweet是地理编码的,我已经用True(如果它已经地理编码)或False(如果它没有地理编码)标记了该Tweet。我要做的是检查该列是否为真,打印出该行。否则,如果该行为False,则将其发送到我的for循环中进行地理编码。我将编辑原始帖子以供输入。@David编辑您的帖子以添加这些信息,以供将来可能需要帮助的人使用you@RafaelC保佑你的灵魂,这确实奏效了。我想我把它弄得太难了。非常感谢。我明白了,再次感谢!:)<代码>df.loc[~df['coords','coords']?