Skip to content

Tag: python

I need help understanding the problem here

I’m trying to make an age calculator that is accurate enough to calculate age based on whether the person has had their birth day this year or not. The problem is something is wrong with my code so even thought when I input “No” it should deduct -1 from the age result but it doesn’t do…

Capture python output to variable

I’m trying to automate pybaseball from player lookup to data extraction and can’t seem to get past the first step of setting the player id as a variable. I’m utilizing the following code to pull player id: The output from the above lines are (exact copy/paste): Is there a method for setting …

Python deque maxlen does not show

I have result but in book Fluent Python (2019), I see maxlen, like this Is the different cause by version different? Answer This seems to be caused by IPython. If you do print(dq) or print(repr(dq)), you get your expected output, and same in a normal REPL. Update: I’ve submitted a PR to IPython to fix t…

Add missing rows in pandas DataFrame

I have a DataFrame that looks like this: What I want to get is: In short, for each id, add the time rows missing with value 0. How do I do this? I wrote something with a loop, but it’s going to be prohibitively slow for my use case which has several million rows Answer Here’s one way using groupby…

Replacing day in date with last day of month

I’ve got a dataframe with column ‘date’, containing yyyy-mm-dd 00:00:00. Python automatically assigned first day of every quarterly month, but I want last day of same month. I’ve tried: and without any success. sample of df: date value 1990-07-01 00:00:00 46.7 1990-10-01 00:00:00 54.2 …