如何解决"ValueError: Shape of passed values is (228, 1), indices imply (228, 7)"



我正在尝试完成课程的作业,并且很难使我的代码的某些方面正常工作。

我正在使用四方形API来探索波特兰的社区,并找到哪些社区有书店(和类似的场所(。我即将执行它,但我一直在标记上面的 ValueError,并且不知道如何重组我的 DF 以使其具有正确的形状。

这是我的代码:

def getNearbyVenues(names, latitudes, longitudes, limit=500):
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
print(name)
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&ll={},{}&v={}&query={}&radius={}&limit={}'.format(
CLIENT_ID, 
CLIENT_SECRET, 
VERSION,
lat,
lng,
query, 
radius, 
LIMIT)
try:
results = requests.get(url, "none").json()['response']['groups'][0]['items']

venues_list.extend([(
name, 
lat, 
lng, 
v['venue']['name'], 
v['venue']['location']['lat'], 
v['venue']['location']['lng'],  
v['venue']['categories'][0]['name']) for v in results])
except KeyError:
venues_list.extend([
name,
lat,
lng]) 
nearby_venues = pd.DataFrame(venues_list, columns = ['Neighborhood', 
'Neighborhood Latitude', 
'Neighborhood Longitude', 
'Venue', 
'Venue Latitude', 
'Venue Longitude', 
'Venue Category'])
return(nearby_venues)
*** NEXT CELL***
PDX_venues = getNearbyVenues(names=PDX_NeighDF['Neighborhood'],
latitudes=PDX_NeighDF['Latitude'],
longitudes=PDX_NeighDF['Longitude']
)
PDX_venues.head()

我在课程中从以前的实验室中获取了原始功能,因此我知道它应该可以工作。第一个单元格实际上执行正常,但是在我尝试使用定义的函数的下一个单元格中标记了 ValueError。这让我认为问题一定出在函数本身。我不得不为我的独特任务对其进行一些修改,因此这可能是问题的一部分。以下是我在基本输入之外对函数所做的更改:

  1. 波特兰的一些社区在我的 API 请求中没有任何返回的场地,并且标记了"KeyError",因为它们在返回的 json 中没有"组"部分,所以我不得不添加"尝试/例外"行除了没有返回场地的社区。当我第一次运行它并允许单元格执行时,这工作正常。但是,在下一个单元格中使用该函数会导致"ValueError"标记。

  2. 试图通过在与我有类似问题的堆栈溢出帖子中的另一个用户的建议下将 venues_list.append 更改为 .extend 来修复下一个单元格中的 ValueError。似乎没有多大帮助,因为我仍然收到 ValueError,但在它被标记为具有一个元素而不是 7 个元素之前(相同的问题,不同的形式(。

任何帮助将不胜感激,这是一个艰难的项目,我能够利用自己的资源走得很远,但这个问题完全难倒了我。提前谢谢你!

此外,以下是完整的 ValueError 回溯:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)
1677                 blocks = [
-> 1678                     make_block(values=blocks[0], placement=slice(0, len(axes[0])))
1679                 ]
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/blocks.py in make_block(values, placement, klass, ndim, dtype, fastpath)
3283 
-> 3284     return klass(values, ndim=ndim, placement=placement)
3285 
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim)
2791 
-> 2792         super().__init__(values, ndim=ndim, placement=placement)
2793 
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/blocks.py in __init__(self, values, placement, ndim)
127                 "Wrong number of items passed {val}, placement implies "
--> 128                 "{mgr}".format(val=len(self.values), mgr=len(self.mgr_locs))
129             )
ValueError: Wrong number of items passed 1, placement implies 7
During handling of the above exception, another exception occurred:
ValueError                                Traceback (most recent call last)
<ipython-input-40-66be6cf8c7d3> in <module>
1 PDX_venues = getNearbyVenues(names=PDX_NeighDF['Neighborhood'],
2                              latitudes=PDX_NeighDF['Latitude'],
----> 3                              longitudes=PDX_NeighDF['Longitude']
4                              )
5 
<ipython-input-39-53e5a94bf83d> in getNearbyVenues(names, latitudes, longitudes, limit)
39                   'Venue Latitude',
40                   'Venue Longitude',
---> 41                   'Venue Category'])
42 
43     return(nearby_venues)
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
462                     mgr = arrays_to_mgr(arrays, columns, index, columns, dtype=dtype)
463                 else:
--> 464                     mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
465             else:
466                 mgr = init_dict({}, index, columns, dtype=dtype)
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/construction.py in init_ndarray(values, index, columns, dtype, copy)
211         block_values = [values]
212 
--> 213     return create_block_manager_from_blocks(block_values, [columns, index])
214 
215 
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/managers.py in create_block_manager_from_blocks(blocks, axes)
1686         blocks = [getattr(b, "values", b) for b in blocks]
1687         tot_items = sum(b.shape[0] for b in blocks)
-> 1688         construction_error(tot_items, blocks[0].shape[1:], axes, e)
1689 
1690 
~/conda/envs/python/lib/python3.6/site-packages/pandas/core/internals/managers.py in construction_error(tot_items, block_shape, axes, e)
1717         raise ValueError("Empty data passed with indices specified.")
1718     raise ValueError(
-> 1719         "Shape of passed values is {0}, indices imply {1}".format(passed, implied)
1720     )
1721 
ValueError: Shape of passed values is (228, 1), indices imply (228, 7)

问题可能归结为您的except子句,该子句不像try子句那样包装在嵌入式元组中。因此,venues_list扩展基础集合的每个元素。因此,您无需[(tuple of vals), tuple(val1, val2, val3), (tuple of vals)][(tuple of vals), val1, val2, val3, (tuple of vals)]传递到构造函数DataFrame

except KeyError:
venues_list.extend([(
name,
lat,
lng)]) 

还要考虑捕获任何异常,这些异常可能是由于 API 请求调用造成的,而不仅仅是返回的 json。此外,包括缺失列的numpy.nan(以便在数据框中NaN而不是None结果(。

except Exception as e:
print(e.message)
venues_list.extend([(
name,
lat,
lng,
np.nan,
np.nan,
np.nan,
np.nan)]) 

相关内容

最新更新