Create pandas dataframe where each cell basis on slope calculation with time series rows from another df











up vote
2
down vote

favorite
2












I have a dataframe with about 40 columns and about 100000 rows:



ID MONTH_NUM_
FROM_EVENT F1 F2 F3 F4 etc…
2 1 4.0 133.0 28.0 NaN
2 2 NaN 132.0 29.0 24.0
2 3 NaN 131.0 NaN 29.0
2 4 4.0 130.0 31.0 7.0
2 5 8.0 129.0 26.0 2.0
2 6 8.0 128.0 25.0 3.0
4 1 5.0 139.0 29.0 7.0
4 2 5.0 138.0 NaN 22.0
4 3 5.0 137.0 30.0 28.0
4 4 5.0 136.0 29.0 25.0
4 5 5.0 135.0 NaN 27.0
4 6 5.0 134.0 27.0 29.0


etc…



each columns F is a 6m time series data with NaN for each rows ID client



I want to output new dataframe without monthes like so:



    ID  F1  F2  F3  F4  etc…
2
4


etc…



where each cell of new data frame is a slope calculation of 6m time series for each F colums with following code example:



x = [6, 5, 4, 3, 2, 1] #its constanta for each calcul, monthes with reverse orders because 1 is last month before event prediction
y = df.F1[df['ID']==2]

xm = np.ma.masked_array(x,mask=np.isnan(y)).compressed() #ignore Nans
ym = np.ma.masked_array(y,mask=np.isnan(y)).compressed() #ignore Nans
from scipy.stats import linregress
linregress(xm, ym).slope


What is the efficient way to looping this calculation and create new df?
Thanx in advance...










share|improve this question




























    up vote
    2
    down vote

    favorite
    2












    I have a dataframe with about 40 columns and about 100000 rows:



    ID MONTH_NUM_
    FROM_EVENT F1 F2 F3 F4 etc…
    2 1 4.0 133.0 28.0 NaN
    2 2 NaN 132.0 29.0 24.0
    2 3 NaN 131.0 NaN 29.0
    2 4 4.0 130.0 31.0 7.0
    2 5 8.0 129.0 26.0 2.0
    2 6 8.0 128.0 25.0 3.0
    4 1 5.0 139.0 29.0 7.0
    4 2 5.0 138.0 NaN 22.0
    4 3 5.0 137.0 30.0 28.0
    4 4 5.0 136.0 29.0 25.0
    4 5 5.0 135.0 NaN 27.0
    4 6 5.0 134.0 27.0 29.0


    etc…



    each columns F is a 6m time series data with NaN for each rows ID client



    I want to output new dataframe without monthes like so:



        ID  F1  F2  F3  F4  etc…
    2
    4


    etc…



    where each cell of new data frame is a slope calculation of 6m time series for each F colums with following code example:



    x = [6, 5, 4, 3, 2, 1] #its constanta for each calcul, monthes with reverse orders because 1 is last month before event prediction
    y = df.F1[df['ID']==2]

    xm = np.ma.masked_array(x,mask=np.isnan(y)).compressed() #ignore Nans
    ym = np.ma.masked_array(y,mask=np.isnan(y)).compressed() #ignore Nans
    from scipy.stats import linregress
    linregress(xm, ym).slope


    What is the efficient way to looping this calculation and create new df?
    Thanx in advance...










    share|improve this question


























      up vote
      2
      down vote

      favorite
      2









      up vote
      2
      down vote

      favorite
      2






      2





      I have a dataframe with about 40 columns and about 100000 rows:



      ID MONTH_NUM_
      FROM_EVENT F1 F2 F3 F4 etc…
      2 1 4.0 133.0 28.0 NaN
      2 2 NaN 132.0 29.0 24.0
      2 3 NaN 131.0 NaN 29.0
      2 4 4.0 130.0 31.0 7.0
      2 5 8.0 129.0 26.0 2.0
      2 6 8.0 128.0 25.0 3.0
      4 1 5.0 139.0 29.0 7.0
      4 2 5.0 138.0 NaN 22.0
      4 3 5.0 137.0 30.0 28.0
      4 4 5.0 136.0 29.0 25.0
      4 5 5.0 135.0 NaN 27.0
      4 6 5.0 134.0 27.0 29.0


      etc…



      each columns F is a 6m time series data with NaN for each rows ID client



      I want to output new dataframe without monthes like so:



          ID  F1  F2  F3  F4  etc…
      2
      4


      etc…



      where each cell of new data frame is a slope calculation of 6m time series for each F colums with following code example:



      x = [6, 5, 4, 3, 2, 1] #its constanta for each calcul, monthes with reverse orders because 1 is last month before event prediction
      y = df.F1[df['ID']==2]

      xm = np.ma.masked_array(x,mask=np.isnan(y)).compressed() #ignore Nans
      ym = np.ma.masked_array(y,mask=np.isnan(y)).compressed() #ignore Nans
      from scipy.stats import linregress
      linregress(xm, ym).slope


      What is the efficient way to looping this calculation and create new df?
      Thanx in advance...










      share|improve this question















      I have a dataframe with about 40 columns and about 100000 rows:



      ID MONTH_NUM_
      FROM_EVENT F1 F2 F3 F4 etc…
      2 1 4.0 133.0 28.0 NaN
      2 2 NaN 132.0 29.0 24.0
      2 3 NaN 131.0 NaN 29.0
      2 4 4.0 130.0 31.0 7.0
      2 5 8.0 129.0 26.0 2.0
      2 6 8.0 128.0 25.0 3.0
      4 1 5.0 139.0 29.0 7.0
      4 2 5.0 138.0 NaN 22.0
      4 3 5.0 137.0 30.0 28.0
      4 4 5.0 136.0 29.0 25.0
      4 5 5.0 135.0 NaN 27.0
      4 6 5.0 134.0 27.0 29.0


      etc…



      each columns F is a 6m time series data with NaN for each rows ID client



      I want to output new dataframe without monthes like so:



          ID  F1  F2  F3  F4  etc…
      2
      4


      etc…



      where each cell of new data frame is a slope calculation of 6m time series for each F colums with following code example:



      x = [6, 5, 4, 3, 2, 1] #its constanta for each calcul, monthes with reverse orders because 1 is last month before event prediction
      y = df.F1[df['ID']==2]

      xm = np.ma.masked_array(x,mask=np.isnan(y)).compressed() #ignore Nans
      ym = np.ma.masked_array(y,mask=np.isnan(y)).compressed() #ignore Nans
      from scipy.stats import linregress
      linregress(xm, ym).slope


      What is the efficient way to looping this calculation and create new df?
      Thanx in advance...







      python pandas dataframe regression






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 9:12

























      asked Nov 21 at 14:27









      Jungleman Jungleman

      112




      112





























          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',
          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%2f53414261%2fcreate-pandas-dataframe-where-each-cell-basis-on-slope-calculation-with-time-ser%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%2f53414261%2fcreate-pandas-dataframe-where-each-cell-basis-on-slope-calculation-with-time-ser%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

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...