1

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'>".
Nayana Madhu
  • 1,185
  • 5
  • 17
  • 34
  • it seems you need remove `*` - `genre(option,option_type,limit)` – jezrael Mar 15 '17 at 08:47
  • 1
    You also have an additional closing bracket in the return line, probably a typo. I would add that as the function stands no one can run or define it as there are missing items to make it function, such as rank_data does not exist. try and make the question complete otherwise solving can be difficult if there are too many unknowns in the snippet to begin with. – grail Mar 15 '17 at 08:49
  • but when I'm importing this function in another file I'm getting an error as ''genre() takes 2 positional arguments but 3 were given''. In order to avoid this error I used * – Nayana Madhu Mar 15 '17 at 09:04
  • I've inserted the image of my rank_data in ''image'' – Nayana Madhu Mar 15 '17 at 09:15

2 Answers2

1

I think you need remove * from *limit if argument limit is 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:

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Consider the dataframe 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 :.

Also, the unpacking of the *limit returns a tuple, we'll want a list.

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
piRSquared
  • 285,575
  • 57
  • 475
  • 624