【发布时间】:2018-03-19 22:27:56
【问题描述】:
我正在尝试在“for”循环中使用“if”语句来检查循环中当前项目的索引(包含该项目的熊猫系列的索引)是否对应于以下索引之一另一个系列,但这样做会引发 ValueError。 这是给出问题的代码行:
if(ICM_items[ICM_items['track_id'] == i].index[0] in ICM_tgt_items.index.values.flatten().tolist()):
我尝试用随机整数或列表更改“in”语句的两侧,它可以工作,这两项也正确构建,但在语句中耦合时会引发错误。
希望有人能给我一些提示,说明问题出在哪里或执行相同任务的替代方法。
ICM_items 和 ICM_tgt_items 都是 pandas.Series
下面是控制台错误:
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]
【问题讨论】:
-
您的问题的设置不是很清楚。如果您提供有代表性的样本数据会有所帮助。见How to create a Minimal, Complete, and Verifiable Example。另外,您确定只想查看与
track_id == i匹配的ICM_items的第一个索引吗?如果返回多个索引怎么办?
标签: python pandas valueerror