我试图在“for”循环内使用“if”语句来检查循环中当前项的索引(包含该项的pandas系列的索引)是否与另一个系列的索引之一对应,但这样做会产生一个ValueError。 这一行代码给出了问题:
if(ICM_items[ICM_items['track_id'] == i].index[0] in ICM_tgt_items.index.values.flatten().tolist()):
我试着用随机整数或列表来改变“in”语句的两边,它也能正常工作,而且这两个项的构建是正确的,但是当它们在语句中耦合时会产生一个错误。
希望有人能给我一些提示,告诉我哪里出了问题,或者是执行相同任务的另一种方法。
ICM_项和ICM_tgt_项都是熊猫
下面是控制台错误:
Traceback (most recent call last):
File "/Users/LucaButera/git/rschallenge/similarity_to_recommandable_builder.py", line 27, in <module>
dot[ICM_tgt_items[ICM_items[ICM_items['track_id'] == i].index[0]]] = 0
File "/Users/LucaButera/anaconda/lib/python3.6/site-packages/pandas/core/series.py", line 603, in __getitem__
result = self.index.get_value(self, key)
File "/Users/LucaButera/anaconda/lib/python3.6/site-packages/pandas/indexes/base.py", line 2169, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas/index.pyx", line 98, in pandas.index.IndexEngine.get_value (pandas/index.c:3557)
File "pandas/index.pyx", line 106, in pandas.index.IndexEngine.get_value (pandas/index.c:3240)
File "pandas/index.pyx", line 147, in pandas.index.IndexEngine.get_loc (pandas/index.c:4194)
File "pandas/index.pyx", line 280, in pandas.index.IndexEngine._ensure_mapping_populated (pandas/index.c:6150)
File "pandas/src/hashtable_class_helper.pxi", line 446, in pandas.hashtable.Int64HashTable.map_locations (pandas/hashtable.c:9261)
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
[Finished in 1.26s]
我建议您简化表达式,使用
.loc
,并注意边缘情况(例如,track_id
对于给定的i
显示为空)。有了正确的测试数据,这些步骤将帮助您缩小bug搜索范围。
示例
ICM_items
数据:示例
ICM_tgt_items
数据:现在简单比较并捕捉潜在的边缘情况:
输出:
相关问题 更多 >
编程相关推荐