I have a dataset that counts the total sum of cases daily, but I want to change it so that it only counts the new cases and not the sum. Each column is a date. Is there a way I can do this?

Hello,

I have a dataset that counts the total sum of cases daily, but I want to change it so that it only counts the new cases and not the sum. Each column is a date. Is there a way I can do this?


This topic was automatically generated from Slack. You can find the original thread here.

Hi @Irwin_Mier_San_Diego_State_University , it sounds like you may want to simply subtract the previous day’s total from each day? It’s hard to know unless you can share some more info, such as a screenshot of what the data set looks like

Yes, I can go ahead and do that

Here is mine. And then afterwards, I want to divide the new cases by the zip_pop for every column.

  1. First, filter out the zip codes with zero population. Check out this link for a way to do that.
  2. Next, assuming all the columns are in order by date, you should be able to for loop through them in reverse order (starting with 9/28/21) and subtracting the column directly to the left, then divide by the zip_pop column. Check out method #5 at this link to see how to loop through the columns by index.

Hi @Ryan_Kruse_MN_State Thank you! I also forgot to mention, I also want to change any negative value I may encounter to zero. If that is also possible

No problem! It’s definitely possible! Check out the pandas.replace function here

So index using iloc is what I can use to subtract the column from the left? And also to dvide?

Yes - index using iloc will help you to select the columns you want to subtract & divide. You should be able to find some examples of the subtraction or division if you search something like “pandas columnwise subtraction”

ah ok great. I also forgot to mention, the first columns are postal_codes, which I dont want to remove, but I want the loop to ignore them. Like using a continue method

Yep, you should be able to work around that with an if statement or something

Im trying to do that, Im not sure if its working atm

mine is

if df[‘postal_code’]:
continue

To prevent any further questions from being overlooked, I’ll go ahead and close this thread out. If you have any more questions or follow-up questions, we’re always here to help! Just be sure to make a new post to help, as we aren’t monitoring old threads at this time. Thanks!