I am trying to find an effective way to generate moving numbers or amounts in pandas, given grouping and date range. In the end, I want to be able to add conditions, i.e. evaluating a type field, but I'm not there yet. I wrote something to do the job, but I feel that there may be a more direct way to achieve the desired result.
My pandas data frame currently looks like this, with the desired output being placed in the last rolling_sales_180 column.
name date amount rolling_sales_180 0 David 2015-01-01 100 100.0 1 David 2015-01-05 500 600.0 2 David 2015-05-30 50 650.0 3 David 2015-07-25 50 100.0 4 Ryan 2014-01-04 100 100.0 5 Ryan 2015-01-19 500 500.0 6 Ryan 2016-03-31 50 50.0 7 Joe 2015-07-01 100 100.0 8 Joe 2015-09-09 500 600.0 9 Joe 2015-10-15 50 650.0
My current solution and environment can be found below. I modeled my solution from this R & A on stackoverflow. Effective way to run subtotal for last 365 days
import pandas as pd import numpy as np def trans_date_to_dist_matrix(date_col):
Your time and feedback are welcome.
python numpy pandas
rs311
source share