Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sum 1 column based from date selection in Pandas

I am trying to figure out a way to do this sum in one line, or without having to create another dataframe in memory.

I have a DF with 3 columns. [‘DateCreated’, ‘InvoiceNumber’, ‘InvoiceAmount’]

I am trying to SUM the invoice amount during certain date ranges.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have this working, but I want to do it without having to create a DF then sum the column. Any help is appreciated.

        yesterday_sales_df = df[(df['DateCreated'] > yesterday_date) & (df['DateCreated'] < tomorrow_date)]
        yesterday_sales_total = yesterday_sales_df['InvoiceAmount'].sum()
        print(yesterday_sales_total)

Thanks

>Solution :

You can try with loc

yesterday_sales_total = df.loc[(df['DateCreated'] > yesterday_date) & (df['DateCreated'] < tomorrow_date), 'InvoiceAmount'].sum()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading