我正在尝试创建一个函数,该函数将通过搜索查询为每个位置的周围环境提取四角形位置数据

问题描述 投票:1回答:1

我有一个包含纽约市大学列表的数据集。我正在尝试创建一个函数,该函数将通过搜索查询为每个位置的周围环境提取Foursquare场地数据,并创建一个新的数据框,该数据框将是纽约大学和Foursquare场地的组合。这是我的大学数据框的示例。

College                                       # Students    Latitude    Longitude
CUNY Borough of Manhattan Community College     26,606      40.7172     -74.0122
Suffolk County Community College                26,600      40.8486     -73.059
Nassau Community College                        22,374      40.0548     -86.943

我用于从Foursquare获取数据的功能是:

def getNearbyhotspots(names, latitudes, longitudes, radius=50):

search_query = 'pubs'
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
    print(name)

    # create the API request URL
    url = 'https://api.foursquare.com/v2/venues/search?client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}'.\
    format(CLIENT_ID,
           CLIENT_SECRET,
           lat,
           lng,
           VERSION,
           search_query,
           radius,
           )

    # make the GET request
    results = requests.get(url).json()['response']['venues']

    # return only relevant information for each nearby venue
    venues_list.append([(
        name, 
        lat, 
        lng, 
        v[0]['name'], 
        v[0]['location']['lat'], 
        v[0]['location']['lng'],  
        v[0]['categories'][0]['name']) for v in results])

nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = ['College', 
              'College Latitude', 
              'College Longitude', 
              'Venue', 
              'Venue Latitude', 
              'Venue Longitude', 
              'Venue Category']
return(nearby_venues)

我正在调用该函数,并收到如下错误:

ny_pubs = getNearbyhotspots(names=cc_df['College'],
                        latitudes=cc_df['Latitude'],
                        longitudes=cc_df['Longitude']
                        )

CUNY Borough of Manhattan Community College
Suffolk County Community College
Nassau Community College
ValueError: Length mismatch: Expected axis has 0 elements, new values have 7 elements

尽管我创建了一个虚拟数据框“ nearby_venues”,具有7列。

我怀疑问题出在函数中,并附加到了events_list。

随机坐标值的结果输出是这样的:

results = requests.get(url).json()['response']['venues']

[{'id': '561b1281498e2a8bbf3b0fff',
'name': "McAteer's Pub",
'location': {'lat': 41.069234,
'lng': -73.799114,
'labeledLatLngs': [{'label': 'display',
 'lat': 41.069234,
 'lng': -73.799114}],
'distance': 708,
'cc': 'US',
'city': 'Greenburgh',
'state': 'NY',
'country': 'United States',
'formattedAddress': ['Greenburgh, NY', 'United States']},
'categories': [{'id': '4bf58dd8d48988d116941735',
'name': 'Bar',
'pluralName': 'Bars',
'shortName': 'Bar',
'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/nightlife/pub_',
 'suffix': '.png'},
'primary': True}],
'referralId': 'v-1590678183',
'hasPerk': False}]
python machine-learning cluster-analysis data-science foursquare
1个回答
0
投票

几件事,一个熊猫可以直接读取JSON并从中制作数据框架。我会检查一下。您正在使用的数据看起来格式正确。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html

否则,此行

nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])

应该可能被重写为类似的东西

nearby_venues = pd.DataFrame(venues_list, columns = LIST_OF_COLUMNS)

因为大熊猫可以使用一个记录列表,并在其形状合理的情况下从中创建数据框。

© www.soinside.com 2019 - 2024. All rights reserved.