How to group date-indexed data and extract timeseries information












0














Working with simplified student sample data that looks like this:



    Date  |  Loc  |  SID  |  Test  |  Score
----------------------------------------------
2018-03-01 L1 S1 T1 3
2018-03-01 L1 S1 T1 5
2018-03-01 L2 S3 T1 3
2018-03-03 L2 S3 T2 4
2018-03-03 L1 S2 T1 1
2018-03-03 L1 S1 T2 5
2018-03-03 L1 S1 T1 4
2018-03-03 L1 S2 T3 7
2018-03-03 L2 S1 T1 5
2018-03-05 L1 S2 T2 3
2018-03-05 L2 S1 T1 1
2018-03-05 L1 S3 T2 5
2018-03-05 L1 S2 T1 8
2018-03-05 L1 S1 T1 6
2018-03-05 L2 S1 T1 3
2018-03-05 L2 S3 T3 5
2018-03-08 L2 S2 T2 4
2018-03-08 L2 S1 T2 2
2018-03-09 L1 S3 T1 6
2018-03-09 L2 S3 T1 5
2018-03-09 L1 S1 T3 8
2018-03-09 L1 S1 T3 6
2018-03-11 L1 S3 T2 6
2018-03-11 L2 S3 T1 9
2018-03-11 L1 S3 T2 3
2018-03-11 L1 S1 T1 5
2018-03-11 L2 S1 T1 4
2018-03-11 L1 S1 T3 9
2018-03-14 L2 S2 T1 3
2018-03-14 L1 S2 T1 3


Would like to groupby (Loc, SID, Test) and calculate the Average Score and Weighted Average Score based on a weekly re-sample so it looks something like the following (not complete, only showing Week 1):



                    | # Times Test Taken  |  Avg. Score  |  Wgtd Avg. Score      
------------|------------------------------------------------------
Week 1| L1 S1 T1 | 4 | 4.50 |
T2 | 1 | 5.00 |
S2 T1 | 2 | 4.50 |
T2 | 1 | 3.00 |
T3 | 1 | 7.00 |
S3 T2 | 1 | 5.00 |
L2 S1 T1 | 3 | 3.00 |
S3 T1 | 1 | 4.00


So far I've:



import pandas as pd

df = pd.read_csv(TheData)
df2 = df.copy()

df2.Date = pd.to_datetime(df2.Date)
df2.set_index('Date', inplace=True)

df3 = df2.copy()
df3.groupby(['Loc', 'SID', 'Test']).resample('W')['Score'].count()
# df3.groupby(['Loc', 'SID', 'Test']).resample('W').count()

df3.groupby(['Loc', 'SID', 'Test']).resample('W').mean()


I believe I have the correct info for "# Times Test Taken" and "Average Score". How can I feed this info into new columns into the same dataframe?



For the weighted avg. score, I'm open to suggestions on how to calculate it such that it can reflect differences in Test Type (T1-T3) as it pertains to score. I'm not even sure that I'm even thinking about this metric the right way.



Will continue to update as I make progress. Any feedback is greatly appreciated.










share|improve this question





























    0














    Working with simplified student sample data that looks like this:



        Date  |  Loc  |  SID  |  Test  |  Score
    ----------------------------------------------
    2018-03-01 L1 S1 T1 3
    2018-03-01 L1 S1 T1 5
    2018-03-01 L2 S3 T1 3
    2018-03-03 L2 S3 T2 4
    2018-03-03 L1 S2 T1 1
    2018-03-03 L1 S1 T2 5
    2018-03-03 L1 S1 T1 4
    2018-03-03 L1 S2 T3 7
    2018-03-03 L2 S1 T1 5
    2018-03-05 L1 S2 T2 3
    2018-03-05 L2 S1 T1 1
    2018-03-05 L1 S3 T2 5
    2018-03-05 L1 S2 T1 8
    2018-03-05 L1 S1 T1 6
    2018-03-05 L2 S1 T1 3
    2018-03-05 L2 S3 T3 5
    2018-03-08 L2 S2 T2 4
    2018-03-08 L2 S1 T2 2
    2018-03-09 L1 S3 T1 6
    2018-03-09 L2 S3 T1 5
    2018-03-09 L1 S1 T3 8
    2018-03-09 L1 S1 T3 6
    2018-03-11 L1 S3 T2 6
    2018-03-11 L2 S3 T1 9
    2018-03-11 L1 S3 T2 3
    2018-03-11 L1 S1 T1 5
    2018-03-11 L2 S1 T1 4
    2018-03-11 L1 S1 T3 9
    2018-03-14 L2 S2 T1 3
    2018-03-14 L1 S2 T1 3


    Would like to groupby (Loc, SID, Test) and calculate the Average Score and Weighted Average Score based on a weekly re-sample so it looks something like the following (not complete, only showing Week 1):



                        | # Times Test Taken  |  Avg. Score  |  Wgtd Avg. Score      
    ------------|------------------------------------------------------
    Week 1| L1 S1 T1 | 4 | 4.50 |
    T2 | 1 | 5.00 |
    S2 T1 | 2 | 4.50 |
    T2 | 1 | 3.00 |
    T3 | 1 | 7.00 |
    S3 T2 | 1 | 5.00 |
    L2 S1 T1 | 3 | 3.00 |
    S3 T1 | 1 | 4.00


    So far I've:



    import pandas as pd

    df = pd.read_csv(TheData)
    df2 = df.copy()

    df2.Date = pd.to_datetime(df2.Date)
    df2.set_index('Date', inplace=True)

    df3 = df2.copy()
    df3.groupby(['Loc', 'SID', 'Test']).resample('W')['Score'].count()
    # df3.groupby(['Loc', 'SID', 'Test']).resample('W').count()

    df3.groupby(['Loc', 'SID', 'Test']).resample('W').mean()


    I believe I have the correct info for "# Times Test Taken" and "Average Score". How can I feed this info into new columns into the same dataframe?



    For the weighted avg. score, I'm open to suggestions on how to calculate it such that it can reflect differences in Test Type (T1-T3) as it pertains to score. I'm not even sure that I'm even thinking about this metric the right way.



    Will continue to update as I make progress. Any feedback is greatly appreciated.










    share|improve this question



























      0












      0








      0







      Working with simplified student sample data that looks like this:



          Date  |  Loc  |  SID  |  Test  |  Score
      ----------------------------------------------
      2018-03-01 L1 S1 T1 3
      2018-03-01 L1 S1 T1 5
      2018-03-01 L2 S3 T1 3
      2018-03-03 L2 S3 T2 4
      2018-03-03 L1 S2 T1 1
      2018-03-03 L1 S1 T2 5
      2018-03-03 L1 S1 T1 4
      2018-03-03 L1 S2 T3 7
      2018-03-03 L2 S1 T1 5
      2018-03-05 L1 S2 T2 3
      2018-03-05 L2 S1 T1 1
      2018-03-05 L1 S3 T2 5
      2018-03-05 L1 S2 T1 8
      2018-03-05 L1 S1 T1 6
      2018-03-05 L2 S1 T1 3
      2018-03-05 L2 S3 T3 5
      2018-03-08 L2 S2 T2 4
      2018-03-08 L2 S1 T2 2
      2018-03-09 L1 S3 T1 6
      2018-03-09 L2 S3 T1 5
      2018-03-09 L1 S1 T3 8
      2018-03-09 L1 S1 T3 6
      2018-03-11 L1 S3 T2 6
      2018-03-11 L2 S3 T1 9
      2018-03-11 L1 S3 T2 3
      2018-03-11 L1 S1 T1 5
      2018-03-11 L2 S1 T1 4
      2018-03-11 L1 S1 T3 9
      2018-03-14 L2 S2 T1 3
      2018-03-14 L1 S2 T1 3


      Would like to groupby (Loc, SID, Test) and calculate the Average Score and Weighted Average Score based on a weekly re-sample so it looks something like the following (not complete, only showing Week 1):



                          | # Times Test Taken  |  Avg. Score  |  Wgtd Avg. Score      
      ------------|------------------------------------------------------
      Week 1| L1 S1 T1 | 4 | 4.50 |
      T2 | 1 | 5.00 |
      S2 T1 | 2 | 4.50 |
      T2 | 1 | 3.00 |
      T3 | 1 | 7.00 |
      S3 T2 | 1 | 5.00 |
      L2 S1 T1 | 3 | 3.00 |
      S3 T1 | 1 | 4.00


      So far I've:



      import pandas as pd

      df = pd.read_csv(TheData)
      df2 = df.copy()

      df2.Date = pd.to_datetime(df2.Date)
      df2.set_index('Date', inplace=True)

      df3 = df2.copy()
      df3.groupby(['Loc', 'SID', 'Test']).resample('W')['Score'].count()
      # df3.groupby(['Loc', 'SID', 'Test']).resample('W').count()

      df3.groupby(['Loc', 'SID', 'Test']).resample('W').mean()


      I believe I have the correct info for "# Times Test Taken" and "Average Score". How can I feed this info into new columns into the same dataframe?



      For the weighted avg. score, I'm open to suggestions on how to calculate it such that it can reflect differences in Test Type (T1-T3) as it pertains to score. I'm not even sure that I'm even thinking about this metric the right way.



      Will continue to update as I make progress. Any feedback is greatly appreciated.










      share|improve this question















      Working with simplified student sample data that looks like this:



          Date  |  Loc  |  SID  |  Test  |  Score
      ----------------------------------------------
      2018-03-01 L1 S1 T1 3
      2018-03-01 L1 S1 T1 5
      2018-03-01 L2 S3 T1 3
      2018-03-03 L2 S3 T2 4
      2018-03-03 L1 S2 T1 1
      2018-03-03 L1 S1 T2 5
      2018-03-03 L1 S1 T1 4
      2018-03-03 L1 S2 T3 7
      2018-03-03 L2 S1 T1 5
      2018-03-05 L1 S2 T2 3
      2018-03-05 L2 S1 T1 1
      2018-03-05 L1 S3 T2 5
      2018-03-05 L1 S2 T1 8
      2018-03-05 L1 S1 T1 6
      2018-03-05 L2 S1 T1 3
      2018-03-05 L2 S3 T3 5
      2018-03-08 L2 S2 T2 4
      2018-03-08 L2 S1 T2 2
      2018-03-09 L1 S3 T1 6
      2018-03-09 L2 S3 T1 5
      2018-03-09 L1 S1 T3 8
      2018-03-09 L1 S1 T3 6
      2018-03-11 L1 S3 T2 6
      2018-03-11 L2 S3 T1 9
      2018-03-11 L1 S3 T2 3
      2018-03-11 L1 S1 T1 5
      2018-03-11 L2 S1 T1 4
      2018-03-11 L1 S1 T3 9
      2018-03-14 L2 S2 T1 3
      2018-03-14 L1 S2 T1 3


      Would like to groupby (Loc, SID, Test) and calculate the Average Score and Weighted Average Score based on a weekly re-sample so it looks something like the following (not complete, only showing Week 1):



                          | # Times Test Taken  |  Avg. Score  |  Wgtd Avg. Score      
      ------------|------------------------------------------------------
      Week 1| L1 S1 T1 | 4 | 4.50 |
      T2 | 1 | 5.00 |
      S2 T1 | 2 | 4.50 |
      T2 | 1 | 3.00 |
      T3 | 1 | 7.00 |
      S3 T2 | 1 | 5.00 |
      L2 S1 T1 | 3 | 3.00 |
      S3 T1 | 1 | 4.00


      So far I've:



      import pandas as pd

      df = pd.read_csv(TheData)
      df2 = df.copy()

      df2.Date = pd.to_datetime(df2.Date)
      df2.set_index('Date', inplace=True)

      df3 = df2.copy()
      df3.groupby(['Loc', 'SID', 'Test']).resample('W')['Score'].count()
      # df3.groupby(['Loc', 'SID', 'Test']).resample('W').count()

      df3.groupby(['Loc', 'SID', 'Test']).resample('W').mean()


      I believe I have the correct info for "# Times Test Taken" and "Average Score". How can I feed this info into new columns into the same dataframe?



      For the weighted avg. score, I'm open to suggestions on how to calculate it such that it can reflect differences in Test Type (T1-T3) as it pertains to score. I'm not even sure that I'm even thinking about this metric the right way.



      Will continue to update as I make progress. Any feedback is greatly appreciated.







      python pandas dataframe time-series weighted-average






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 6:39









      dmitriys

      15119




      15119










      asked Nov 12 at 22:26









      jarwal

      155




      155





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270995%2fhow-to-group-date-indexed-data-and-extract-timeseries-information%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270995%2fhow-to-group-date-indexed-data-and-extract-timeseries-information%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)