find average in remaining days using python pandas












0















df is a csv file that contains ship_date, order_date and cumulative_ordered. cumulative_ordered is the sum of orders added on every day up until ship_date. There are 30 days prior to every ship_date and those days only counts for one ship_date. after ship_date 2018-07-01, then next ship_date will be 2018-08-01 with same procedure.



My problem is that I can't get days remaining(see last code output below) when I calculate the percentage average of cumulative_ordered on each of those 30 days prior.



I have the following code, that gives me the days_remaining from a csv file with several different ship_date and order_date counting down to each separate ship_date.



df['days_remaining'] = pd.to_datetime(df['ship_date']).sub
(pd.to_datetime(df['order_date'])).dt.days
df['difference'] = df['ship_date'] - df['order_date']

df.head()


output:



ship_date    Order_date   cumulative_ordered   days_remaining    difference

2018-07-01 2018-06-01 7 30 30 days
2018-07-01 2018-06-02 10 29 29 days
2018-07-01 2018-06-03 15 28 28 days
2018-07-01 2018-06-04 30 28 27 days
2018-07-01 2018-06-05 41 28 26 days


I then try to find total ordered on each day prior to ship_date



m = df.groupby("difference").mean()
m.head()


which gives me this output:



             cumulative ordered    days_remaining
difference
0 days 352.458124 0.0
1 days 291.234747 1.0
2 days 244.122137 2.0
3 days 201.178765 3.0
4 days 190.153641 4.0


I'm running into an issue when I try to find an average of cumulative ordered on each day in terms of percentage filled from cumulative_ordered output on 0 days above, by running this code:



   v = m/m[m.index.days == 0].iloc[0]
v.head()

cumulative_ordered days_remaining
difference
0 days 1.000000 NaN
1 days 0.891324 inf
2 days 0.812534 inf
3 days 0.752339 inf
4 days 0.673745 inf


days_remaining changes to NaN and inf.. How can I keep it so it still gives me integers?










share|improve this question



























    0















    df is a csv file that contains ship_date, order_date and cumulative_ordered. cumulative_ordered is the sum of orders added on every day up until ship_date. There are 30 days prior to every ship_date and those days only counts for one ship_date. after ship_date 2018-07-01, then next ship_date will be 2018-08-01 with same procedure.



    My problem is that I can't get days remaining(see last code output below) when I calculate the percentage average of cumulative_ordered on each of those 30 days prior.



    I have the following code, that gives me the days_remaining from a csv file with several different ship_date and order_date counting down to each separate ship_date.



    df['days_remaining'] = pd.to_datetime(df['ship_date']).sub
    (pd.to_datetime(df['order_date'])).dt.days
    df['difference'] = df['ship_date'] - df['order_date']

    df.head()


    output:



    ship_date    Order_date   cumulative_ordered   days_remaining    difference

    2018-07-01 2018-06-01 7 30 30 days
    2018-07-01 2018-06-02 10 29 29 days
    2018-07-01 2018-06-03 15 28 28 days
    2018-07-01 2018-06-04 30 28 27 days
    2018-07-01 2018-06-05 41 28 26 days


    I then try to find total ordered on each day prior to ship_date



    m = df.groupby("difference").mean()
    m.head()


    which gives me this output:



                 cumulative ordered    days_remaining
    difference
    0 days 352.458124 0.0
    1 days 291.234747 1.0
    2 days 244.122137 2.0
    3 days 201.178765 3.0
    4 days 190.153641 4.0


    I'm running into an issue when I try to find an average of cumulative ordered on each day in terms of percentage filled from cumulative_ordered output on 0 days above, by running this code:



       v = m/m[m.index.days == 0].iloc[0]
    v.head()

    cumulative_ordered days_remaining
    difference
    0 days 1.000000 NaN
    1 days 0.891324 inf
    2 days 0.812534 inf
    3 days 0.752339 inf
    4 days 0.673745 inf


    days_remaining changes to NaN and inf.. How can I keep it so it still gives me integers?










    share|improve this question

























      0












      0








      0








      df is a csv file that contains ship_date, order_date and cumulative_ordered. cumulative_ordered is the sum of orders added on every day up until ship_date. There are 30 days prior to every ship_date and those days only counts for one ship_date. after ship_date 2018-07-01, then next ship_date will be 2018-08-01 with same procedure.



      My problem is that I can't get days remaining(see last code output below) when I calculate the percentage average of cumulative_ordered on each of those 30 days prior.



      I have the following code, that gives me the days_remaining from a csv file with several different ship_date and order_date counting down to each separate ship_date.



      df['days_remaining'] = pd.to_datetime(df['ship_date']).sub
      (pd.to_datetime(df['order_date'])).dt.days
      df['difference'] = df['ship_date'] - df['order_date']

      df.head()


      output:



      ship_date    Order_date   cumulative_ordered   days_remaining    difference

      2018-07-01 2018-06-01 7 30 30 days
      2018-07-01 2018-06-02 10 29 29 days
      2018-07-01 2018-06-03 15 28 28 days
      2018-07-01 2018-06-04 30 28 27 days
      2018-07-01 2018-06-05 41 28 26 days


      I then try to find total ordered on each day prior to ship_date



      m = df.groupby("difference").mean()
      m.head()


      which gives me this output:



                   cumulative ordered    days_remaining
      difference
      0 days 352.458124 0.0
      1 days 291.234747 1.0
      2 days 244.122137 2.0
      3 days 201.178765 3.0
      4 days 190.153641 4.0


      I'm running into an issue when I try to find an average of cumulative ordered on each day in terms of percentage filled from cumulative_ordered output on 0 days above, by running this code:



         v = m/m[m.index.days == 0].iloc[0]
      v.head()

      cumulative_ordered days_remaining
      difference
      0 days 1.000000 NaN
      1 days 0.891324 inf
      2 days 0.812534 inf
      3 days 0.752339 inf
      4 days 0.673745 inf


      days_remaining changes to NaN and inf.. How can I keep it so it still gives me integers?










      share|improve this question














      df is a csv file that contains ship_date, order_date and cumulative_ordered. cumulative_ordered is the sum of orders added on every day up until ship_date. There are 30 days prior to every ship_date and those days only counts for one ship_date. after ship_date 2018-07-01, then next ship_date will be 2018-08-01 with same procedure.



      My problem is that I can't get days remaining(see last code output below) when I calculate the percentage average of cumulative_ordered on each of those 30 days prior.



      I have the following code, that gives me the days_remaining from a csv file with several different ship_date and order_date counting down to each separate ship_date.



      df['days_remaining'] = pd.to_datetime(df['ship_date']).sub
      (pd.to_datetime(df['order_date'])).dt.days
      df['difference'] = df['ship_date'] - df['order_date']

      df.head()


      output:



      ship_date    Order_date   cumulative_ordered   days_remaining    difference

      2018-07-01 2018-06-01 7 30 30 days
      2018-07-01 2018-06-02 10 29 29 days
      2018-07-01 2018-06-03 15 28 28 days
      2018-07-01 2018-06-04 30 28 27 days
      2018-07-01 2018-06-05 41 28 26 days


      I then try to find total ordered on each day prior to ship_date



      m = df.groupby("difference").mean()
      m.head()


      which gives me this output:



                   cumulative ordered    days_remaining
      difference
      0 days 352.458124 0.0
      1 days 291.234747 1.0
      2 days 244.122137 2.0
      3 days 201.178765 3.0
      4 days 190.153641 4.0


      I'm running into an issue when I try to find an average of cumulative ordered on each day in terms of percentage filled from cumulative_ordered output on 0 days above, by running this code:



         v = m/m[m.index.days == 0].iloc[0]
      v.head()

      cumulative_ordered days_remaining
      difference
      0 days 1.000000 NaN
      1 days 0.891324 inf
      2 days 0.812534 inf
      3 days 0.752339 inf
      4 days 0.673745 inf


      days_remaining changes to NaN and inf.. How can I keep it so it still gives me integers?







      python pandas dataframe percentage






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 19:49









      CluelessClueless

      275




      275
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The NaN and inf result from dividing by 0.0.



          It seems that you are trying to apply the operation only to the cumulative_ordered column, so you should just run this for your last block of code:



          m['cumulative_ordered'] = m['cumulative_ordered'] / m['cumulative_ordered'][m['cumulative_ordered'].index.days == 0]





          share|improve this answer


























          • Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

            – Clueless
            Nov 21 '18 at 21:56











          • What is the desired output for days_remaining?

            – PJW
            Nov 21 '18 at 22:07











          • no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

            – Clueless
            Nov 21 '18 at 22:15













          • Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

            – PJW
            Nov 21 '18 at 22:26













          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%2f53419542%2ffind-average-in-remaining-days-using-python-pandas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The NaN and inf result from dividing by 0.0.



          It seems that you are trying to apply the operation only to the cumulative_ordered column, so you should just run this for your last block of code:



          m['cumulative_ordered'] = m['cumulative_ordered'] / m['cumulative_ordered'][m['cumulative_ordered'].index.days == 0]





          share|improve this answer


























          • Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

            – Clueless
            Nov 21 '18 at 21:56











          • What is the desired output for days_remaining?

            – PJW
            Nov 21 '18 at 22:07











          • no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

            – Clueless
            Nov 21 '18 at 22:15













          • Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

            – PJW
            Nov 21 '18 at 22:26


















          1














          The NaN and inf result from dividing by 0.0.



          It seems that you are trying to apply the operation only to the cumulative_ordered column, so you should just run this for your last block of code:



          m['cumulative_ordered'] = m['cumulative_ordered'] / m['cumulative_ordered'][m['cumulative_ordered'].index.days == 0]





          share|improve this answer


























          • Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

            – Clueless
            Nov 21 '18 at 21:56











          • What is the desired output for days_remaining?

            – PJW
            Nov 21 '18 at 22:07











          • no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

            – Clueless
            Nov 21 '18 at 22:15













          • Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

            – PJW
            Nov 21 '18 at 22:26
















          1












          1








          1







          The NaN and inf result from dividing by 0.0.



          It seems that you are trying to apply the operation only to the cumulative_ordered column, so you should just run this for your last block of code:



          m['cumulative_ordered'] = m['cumulative_ordered'] / m['cumulative_ordered'][m['cumulative_ordered'].index.days == 0]





          share|improve this answer















          The NaN and inf result from dividing by 0.0.



          It seems that you are trying to apply the operation only to the cumulative_ordered column, so you should just run this for your last block of code:



          m['cumulative_ordered'] = m['cumulative_ordered'] / m['cumulative_ordered'][m['cumulative_ordered'].index.days == 0]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 21:36

























          answered Nov 21 '18 at 21:30









          PJWPJW

          638725




          638725













          • Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

            – Clueless
            Nov 21 '18 at 21:56











          • What is the desired output for days_remaining?

            – PJW
            Nov 21 '18 at 22:07











          • no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

            – Clueless
            Nov 21 '18 at 22:15













          • Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

            – PJW
            Nov 21 '18 at 22:26





















          • Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

            – Clueless
            Nov 21 '18 at 21:56











          • What is the desired output for days_remaining?

            – PJW
            Nov 21 '18 at 22:07











          • no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

            – Clueless
            Nov 21 '18 at 22:15













          • Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

            – PJW
            Nov 21 '18 at 22:26



















          Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

          – Clueless
          Nov 21 '18 at 21:56





          Hi, thanks, this is not working because I need the days_remaining column to be able to merge with another csv file and from there make a forecast based on cumulative_ordered average.

          – Clueless
          Nov 21 '18 at 21:56













          What is the desired output for days_remaining?

          – PJW
          Nov 21 '18 at 22:07





          What is the desired output for days_remaining?

          – PJW
          Nov 21 '18 at 22:07













          no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

          – Clueless
          Nov 21 '18 at 22:15







          no change, just for it to remain the same as above.. counting down the days.. 0,1,2 etc

          – Clueless
          Nov 21 '18 at 22:15















          Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

          – PJW
          Nov 21 '18 at 22:26







          Yeah, so I thought if you run the line I suggested, days_remaining would remain unchanged. And if you print m.head(), cumulative_ordered would be the only column modified (if you want days_remaining to be integers instead of floats, that is another issue).

          – PJW
          Nov 21 '18 at 22:26




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419542%2ffind-average-in-remaining-days-using-python-pandas%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

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga