0

having this dataframe:

            provincia     contagios      defunciones    fecha
0   distrito nacional            11                0    18/3/2020
1                azua             0                0    18/3/2020
2             baoruco             0                0    18/3/2020
3             dajabon             0                0    18/3/2020
4            barahona             0                0    18/3/2020

How can I have a new dataframe like this:

            provincia     contagios_from_march1_8        defunciones_from_march1_8  
0   distrito nacional                          11                                0  
1                azua                           0                                0  
2             baoruco                           0                                0  
3             dajabon                           0                                0  
4            barahona                           0                                0  

Where the 'contagios_from_march1_8' and 'defunciones_from_march1_8' are the result of the sum of the 'contagios' and 'defunciones' in the date range 3/1/2020 to 3/8/2020.

Thanks.

Luis Jacobo
  • 97
  • 1
  • 14
  • 2
    your input dataframe doesnot have the date index you are expecting , please edit the example in relevance to the question so that users can copy the data and test. Also post the desired output for validation and the code you have tried. – anky Apr 12 '20 at 16:54
  • I had edited the post with a better explication. Thanks – Luis Jacobo Apr 12 '20 at 17:50

1 Answers1

0

Can use df.sum on a condition. Eg.:

    df[df["date"]<month]["contagios"].sum()

refer to this for extracting month out of date: Extracting just Month and Year separately from Pandas Datetime column

Anchal Gupta
  • 219
  • 1
  • 9