【问题标题】:TypeError: cannot do slice indexing on <class 'pandas.indexes.numeric.Int64Index'> with these indexers [(2,)] of <class 'tuple'>类型错误:无法使用 <class 'tuple'> 的这些索引器 [(2,)] 对 <class 'pandas.indexes.numeric.Int64Index'> 进行切片索引
【发布时间】: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


【解决方案1】:

考虑数据框rank_data

rank_data = pd.DataFrame(dict(
        genre=['Crime'] * 4 + ['Romance'] * 4
    ))

print(rank_data)

     genre
0    Crime
1    Crime
2    Crime
3    Crime
4  Romance
5  Romance
6  Romance
7  Romance

我假设您想获取切片的第二个元素,因为您将 2 传递给您的函数。在这种情况下,我将假设您要使用 iloc 并跳过前面的 :

另外,*limit 的解包返回一个元组,我们需要一个列表。

def genre(option,option_type,*limit):
    option_based = rank_data.loc[rank_data[option] == option_type]
    top_option_based  = option_based.iloc[list(limit)]
               # I changed this bit  ^^^^^^^^^^^^^^^^^
    top_option_based.to_csv('top_option_based.csv')
    return(top_option_based)

genre('genre', 'Crime', 2)

   genre
2  Crime

genre('genre', 'Crime', 2, 3)

   genre
2  Crime
3  Crime

【讨论】:

  • 我不需要第二个元素。我需要最多 2 个元素(限制)
【解决方案2】:

如果参数limitintegerrank_data,我认为您需要从*limit 中删除*

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)

借用另一个答案的样本,效果很好:

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)

print (genre('genre', 'Crime', 2))
   genre
0  Crime
1  Crime

编辑:

我认为您也需要添加 dataframe 作为参数:

def genre(rank_data, 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)

print (genre(rank_data, 'genre', 'Crime', 2))
   genre
0  Crime
1  Crime

【讨论】:

  • 好的,但是当我在另一个文件中导入这个函数时,我收到一个错误,因为''genre() 需要 2 个位置参数,但给出了 3 个参数''。为了避免这个错误,我使用了 *
  • 然后检查另一个答案。
  • 看来您需要添加参数 - 数据框。
  • 试过了,但没有。
  • 是的。 TypeError:genre() 接受 2 个位置参数,但给出了 4 个。当我在新文件中使用该功能时会出现问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 2018-01-09
相关资源
最近更新 更多