0

For example, I have many dates that range from 11/30/2020 to 12/20/2020, and each date has about 300 different values in a column. How can I calculate the difference value for the "max-min" of each date? (for just 1 column)

I thought about for loop but have no idea how to connect them I really need the solution and I appreciate that!!!!!

  • Welcome to Stack Overflow. Are you able to solve the problem for a single date? How are the dates actually represented? "I thought about for loop" You should not; this is Pandas, after all. Half the point of using it, is so that it can loop for you. You should try to [research](https://meta.stackoverflow.com/questions/261592) questions like this first, for example by [using a search engine](https://duckduckgo.com/?q=pandas+date+difference). – Karl Knechtel Mar 29 '22 at 04:35
  • Also: it's not really clear what you mean. Are you saying that there is one column of dates, and you want to find the latest and earliest date in the column, and subtract them? Or are you saying that there are two columns of dates, a max date column and a min date column, and you want to make a new column with the differences? Or something else? – Karl Knechtel Mar 29 '22 at 04:36

1 Answers1

0

You can do a groupby on the date column and fetch the max and min df's individually and then just subtract two columns.

min_df = df.groupby(['date_col']).agg({'value_col':'min'})
max_df = df.groupby(['date_col']).agg({'value_col':'max'})

This solution should help you: pandas groupby where you get the max of one column and the min of another column