31 Years of Python | 48 Hour Sale Extension!!!
days
hours
minutes
seconds

Guided Project: Mobile App for Lottery Addiction_5th step

Hi,

I am kinda stuck in this step here, so maybe someone could give me a hand : ):

Screen Link:
Mobile App For Lottery Addiction — Function For Historical Data Check | Dataquest

My Code for creating a function that extracts all the six winning numbers, given a specific row number:

ls = []

def nmrs_extraidos(c):
    for r in range(0, loto_6_49.shape[0]):
        row = set(number_drawn.loc[r])
        ls.append(row)
    return ls[c]

        
nmrs_extraidos(0)

It returns this:

{3, 11, 12, 14, 41, 43}

But when I try to apply the function to the dataframe:

winning_nmrs = loto_6_49.apply(nmrs_extraidos, axis=1)

I get the following error:

TypeError: ('list indices must be integers, not Series', 'occurred at index 0')

:neutral_face:

Thanks!

At this stage, I would recommend trying to debug the error yourself since you should have enough python experience by now. So, try to answer a few questions:

  • Where does the error originate? The error message usually points to that.
  • Given where it originates, what list might the error message refer to?
  • What does the error message imply? It’s talking about list, indices, integers, Series. Given all of those and the possible line of code that caused the error, what can you infer from the message? What do you think is happening?
  • Lastly, have you tried to google the error (with or without additional keywords you might think are relevant to your code)? What kind of results do you get? How do they relate to your current understanding given the above questions?

Try to answer the above questions and debug it yourself first. Feel free to ask more questions after that.

Hi @the_doctor :slight_smile: ,

For what I can tell the error is being originated in my function…

Hhhmmm, it might be referring to the ls list?

My function is made to return a set of winning numbers upon a specific index/row number. But I have to do a more generic function, so that it can capture all the meaningfull rows, once used with the apply.

I did, and the closest to what I found whas this in stack:

python - Error: list indices must be integers, not Series - Stack Overflow

I am still kinda of clueless…

I think also what is happening is that the Apply function requires Series objects to be passed into it, and I don’t think that that is whats happening. No?

The full error message shows where the error originated. I would suggest going through it to get a grasp of how error messages are structured for future errors.

But, yes the error most likely happens at ls[c].

When you use

axis=1 means that nmrs_extraidos is applied to every row, as you know. That means in the function

c would be an entire row. In Pandas, that would be of type Series.

So, when you use ls[c], you are trying to index a Series which throws the error.

You need to see how you want to return. Because, as I said, c is not a valid index for a list. It’s a Series and not an integer.

You need to look at what index to use to be able to return what you want from ls.