How to count and acummulate values for each rowname and not loosing the DateTime order in Python using pandas

Question:

I got a dataframe that looks like this. What I want to do is to:

  1. Sort my DF by DateTime
  1. After sorting my DF by date, adding a new column that counts and acummulates values for EACH rowname in "Cod_atc".

The problem is that everytime I add this new column, no matter what I do, I can not get my DF sorted by DateTime

This is the code I am using, I am just adding a column called "DateTime" and sorting everything by that column. The problem is when i add the new column called "count".

df1['DateTime'] = pd.to_datetime(df1['local_date'])
df1.sort_values(by='DateTime')
df1['count']=df1.groupby(['cod_atc']).cumcount() #sort=False
df1

enter image description here

This is the result I get and the problem is that, if I try to sort my DF by DateTime again, it works but the "count" column would not make any sense! "Count" column should be counting and acumulating values for EACH rowname in "COD_Atc" but following the DATETIME! enter image description here

Asked By: JN1997

||

Answers:

Did you not forgot to add inplace = True when you sorted df1?
Without that you lose the sort step.

df1['DateTime'] = pd.to_datetime(df1['local_date'])
df1.sort_values(by='DateTime', inplace =True)
df1['count']=df1.groupby(['cod_atc']).cumcount() #sort=False
df1
Answered By: Renaud
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.