Note that there are some explanatory texts on larger screens.

plurals
  1. POGroup by - select most recent 4 events
    primarykey
    data
    text
    <p>I have the following df in pandas:</p> <pre><code>df: DATE STOCK DATA1 DATA2 DATA3 01/01/12 ABC 0.40 0.88 0.22 04/01/12 ABC 0.50 0.49 0.13 07/01/12 ABC 0.85 0.36 0.83 10/01/12 ABC 0.28 0.12 0.39 01/01/13 ABC 0.86 0.87 0.58 04/01/13 ABC 0.95 0.39 0.87 07/01/13 ABC 0.60 0.25 0.56 10/01/13 ABC 0.15 0.28 0.69 01/01/11 XYZ 0.94 0.40 0.50 04/01/11 XYZ 0.65 0.19 0.81 07/01/11 XYZ 0.89 0.59 0.69 10/01/11 XYZ 0.12 0.09 0.18 01/01/12 XYZ 0.25 0.94 0.55 04/01/12 XYZ 0.07 0.22 0.67 07/01/12 XYZ 0.46 0.08 0.54 10/01/12 XYZ 0.04 0.03 0.94 ... </code></pre> <p>I want to group by the stocks, sort by date and then for specified columns (in this case DATA1 and DATA3), I want to get the last four items summed (TTM data).</p> <p>The output would look like this:</p> <pre><code>DATE STOCK DATA1 DATA2 DATA3 DATA1_TTM DATA3_TTM 01/01/12 ABC 0.40 0.88 0.22 NaN NaN 04/01/12 ABC 0.50 0.49 0.13 NaN NaN 07/01/12 ABC 0.85 0.36 0.83 NaN NaN 10/01/12 ABC 0.28 0.12 0.39 2.03 1.56 01/01/13 ABC 0.86 0.87 0.58 2.49 1.92 04/01/13 ABC 0.95 0.39 0.87 2.94 2.66 07/01/13 ABC 0.60 0.25 0.56 2.69 2.39 10/01/13 ABC 0.15 0.28 0.69 2.55 2.70 01/01/11 XYZ 0.94 0.40 0.50 NaN NaN 04/01/11 XYZ 0.65 0.19 0.81 NaN NaN 07/01/11 XYZ 0.89 0.59 0.69 NaN NaN 10/01/11 XYZ 0.12 0.09 0.18 2.59 2.18 01/01/12 XYZ 0.25 0.94 0.55 1.90 2.23 04/01/12 XYZ 0.07 0.22 0.67 1.33 2.09 07/01/12 XYZ 0.46 0.08 0.54 0.89 1.94 10/01/12 XYZ 0.04 0.03 0.94 0.82 2.70 ... </code></pre> <p>My approach so far has been to sort by date, then group, then iterate through each group and if there are 3 older events then the current event I sum. Also, I want to check to see if the dates fall within 1 year. Can anyone offer a better way in Python? Thank you.</p> <p>Added: As a clarification for the 1 year part, let's say you take the last four dates and it goes 1/1/1993, 4/1/12, 7/1/12, 10/1/12 -- a data error. I wouldn't want to sum those four. I would want that one to say NaN.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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