Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For this I think you can use <code>transform</code> and <code>rolling_sum</code>. Starting from your dataframe, I might do something like:</p> <pre><code>&gt;&gt;&gt; df["DATE"] = pd.to_datetime(df["DATE"]) # switch to datetime to ease sorting &gt;&gt;&gt; df = df.sort(["STOCK", "DATE"]) &gt;&gt;&gt; rsum_columns = "DATA1", "DATA3" &gt;&gt;&gt; grouped = df.groupby("STOCK")[rsum_columns] &gt;&gt;&gt; new_columns = grouped.transform(lambda x: pd.rolling_sum(x, 4)) &gt;&gt;&gt; df[new_columns.columns + "_TTM"] = new_columns &gt;&gt;&gt; df DATE STOCK DATA1 DATA2 DATA3 DATA1_TTM DATA3_TTM 0 2012-01-01 00:00:00 ABC 0.40 0.88 0.22 NaN NaN 1 2012-04-01 00:00:00 ABC 0.50 0.49 0.13 NaN NaN 2 2012-07-01 00:00:00 ABC 0.85 0.36 0.83 NaN NaN 3 2012-10-01 00:00:00 ABC 0.28 0.12 0.39 2.03 1.57 4 2013-01-01 00:00:00 ABC 0.86 0.87 0.58 2.49 1.93 5 2013-04-01 00:00:00 ABC 0.95 0.39 0.87 2.94 2.67 6 2013-07-01 00:00:00 ABC 0.60 0.25 0.56 2.69 2.40 7 2013-10-01 00:00:00 ABC 0.15 0.28 0.69 2.56 2.70 8 2011-01-01 00:00:00 XYZ 0.94 0.40 0.50 NaN NaN 9 2011-04-01 00:00:00 XYZ 0.65 0.19 0.81 NaN NaN 10 2011-07-01 00:00:00 XYZ 0.89 0.59 0.69 NaN NaN 11 2011-10-01 00:00:00 XYZ 0.12 0.09 0.18 2.60 2.18 12 2012-01-01 00:00:00 XYZ 0.25 0.94 0.55 1.91 2.23 13 2012-04-01 00:00:00 XYZ 0.07 0.22 0.67 1.33 2.09 14 2012-07-01 00:00:00 XYZ 0.46 0.08 0.54 0.90 1.94 15 2012-10-01 00:00:00 XYZ 0.04 0.03 0.94 0.82 2.70 [16 rows x 7 columns] </code></pre> <p>I don't know what you're asking by "Also, I want to check to see if the dates fall within 1 year", so I'll leave that alone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload