BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Guided Project: Mobile App for Lottery Addiction. .apply() behaves weird on the dataframe

Hi,

https://app.dataquest.io/m/382/guided-project%3A-mobile-app-for-lottery-addiction/7/less-winning-numbers-function

I have imported the .csv and added the parameter parse_dates to column ‘DRAW DATE’ using the following code:

 lottery = pd.read_csv('649.csv', parse_dates = ['DRAW DATE'])

then I have created a function called extract numbers using the following code:

def extract_numbers(row):
    row = row[4:10]
    row = set(row.values)
    return row

The previous function is the same than the function provided on the solution by dataquest.

Then I have applied the previous function to the dataframe using .apply() as follows:

winning_numbers = lottery.apply(extract_numbers, axis=1)
winning_numbers 

This caused to copy the set of 6 numbers on every value on the dataframe instead of extracting the set from the row.

After many hours trying to find the problem, I have solved it removing ‘parse_dates’ parameter from .read_csv.

Do you know why this happens? It is strange that a column not used by .apply, affects its result.

Here is a picture of the dataframe with parse_dates

Regards,

Jorge

Can you please share a notebook showing the behavior you describe?

I have just downloaded it from dataquest, and on my laptop works as spected while on dataquest still shows the dataframe with sets on each value. I will send it anyway, in case you want to try the code on the dataquest platform and see if you get the same behavior

Basics.ipynb (14.4 KB)

Click here to view the jupyter notebook file in a new tab

This seems to be working as expected. Did you send the notebook after running locally? I meant a notebook showcasing the behavior you’re asking about (i.e. from app.dataquest.io).

Can you please add a link to the mission where this occurs? Add it to the question directly.

https://app.dataquest.io/m/382/guided-project%3A-mobile-app-for-lottery-addiction/7/less-winning-numbers-function

Yes, the notebook I have sent is after running it locally but I haven’t change anything on the code.

Yes, but this means that I have to run code in the app myself, which delays you getting an answer. Anyway…

This is absolutely bizarre. I have no idea why this happens, but it seems like it is a bug that was resolved. In app.dataquest.io, we use pandas version 0.22. You’re probably on a much more recent version, hence the difference.

Thank you very much Bruno. I didn’t know if it was something that I was doing wrong.

Regards,

1 Like

Thanks so much for the reply, the same thing happened to me and I spent one hour trying to find out why!

1 Like