类型错误:无法使用 <class 'tuple'> 的这些索引器 [(2,)] 对 <class 'pandas.indexes.numeric.Int64Index'> 进行切片索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42804573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: cannot do slice indexing on <class 'pandas.indexes.numeric.Int64Index'> with these indexers [(2,)] of <class 'tuple'>
提问by Nayana Madhu
I've a user defined function as follows:-
我有一个用户定义的函数,如下所示:-
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))
please refer thisimage
请参考这张图片
when I use the function as
当我使用该函数时
genre('genre','Crime',2)
I'm getting an error as
我收到一个错误
TypeError: cannot do slice indexing on <class 'pandas.indexes.numeric.Int64Index'> with these indexers [(2,)] of <class 'tuple'>".
回答by jezrael
I think you need remove *
from *limit
if argument limit
is integer
and rank_data
:
我认为你需要*
从*limit
if 参数limit
是integer
and 中删除rank_data
:
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)
With borrowing sample with another answer it works perfectly:
借用另一个答案的样本,它可以完美地工作:
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
EDIT:
编辑:
I think you need add dataframe
as argument too:
我认为您也需要添加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
回答by piRSquared
Consider the dataframe rank_data
考虑数据框 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
I'm going to assume you wanted to get the 2nd element of the slice due to your passing a 2
to your function. In that case, I'm going to assume you want to use iloc
and skip the preceding :
.
我将假设您想获得切片的第二个元素,因为您将 a 传递2
给您的函数。在这种情况下,我将假设您想使用iloc
并跳过前面的:
.
Also, the unpacking of the *limit
returns a tuple, we'll want a list.
此外,解包*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