【发布时间】:2017-08-05 21:15:30
【问题描述】:
我有一个用户定义的函数如下:-
def genre(option,option_type,*limit):
option_based = rank_data.loc[rank_data[option] == option_type]
top_option_based = option_based[:limit]
print(top_option_based)
top_option_based.to_csv('top_option_based.csv')
return(top_option_based))
请参考image
当我使用函数时
genre('genre','Crime',2)
我收到一个错误
TypeError: cannot do slice indexing on <class 'pandas.indexes.numeric.Int64Index'> with these indexers [(2,)] of <class 'tuple'>".
【问题讨论】:
-
看来你需要删除
*-genre(option,option_type,limit) -
您在返回行中还有一个额外的右括号,可能是拼写错误。我要补充一点,因为该功能没有人可以运行或定义它,因为缺少使其起作用的项目,例如 rank_data 不存在。尝试完成问题,否则如果 sn-p 中的未知数太多,则很难解决。
-
但是当我在另一个文件中导入这个函数时,我收到一个错误,因为''genre() 需要 2 个位置参数,但给出了 3 个''。为了避免这个错误,我使用了 *
-
我已将我的 rank_data 的图像插入到 ''image'' 中
标签: python python-3.x pandas slice argument-unpacking