Lzypenguin
Lzypenguin

Reputation: 955

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.

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

Upvotes: 1

Views: 55

Answers (3)

cottontail
cottontail

Reputation: 23041

You can use this as well

# filter df with query
yesterday_sales_total = df.query("@yesterday_date < DateCreated < @tomorrow_date")['InvoiceAmount'].sum()

Upvotes: 2

SergFSM
SergFSM

Reputation: 1491

try between:

sales_total = df[df['DateCreated'].between(yesterday_date, tomorrow_date)]['InvoiceAmount'].sum()

if it's nessesary set inclusive argument (inclusive='both' by default)

Upvotes: 1

Ynjxsjmh
Ynjxsjmh

Reputation: 29992

You can try with loc

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

Upvotes: 4

Related Questions