Creating rows as per duration using datetime pandas












1















I am facing an issue to write a code using datetime. I had created a scenario I am working on. Can someone help me out on the code.



Input:



Name, Channel, Duration, Start_time
John, A, 2, 16:00:00
Joseph, B, 3, 15:05:00


Output:



Name, Channel, Duration, Start_time
John, A, 2, 16:00:00
John, A, 2, 16:01:00
Joseph, B, 3, 15:05:00
Joseph, B, 3, 15:06:00
Joseph, B, 3, 15:07:00


Thank you in advance.



enter image description here










share|improve this question





























    1















    I am facing an issue to write a code using datetime. I had created a scenario I am working on. Can someone help me out on the code.



    Input:



    Name, Channel, Duration, Start_time
    John, A, 2, 16:00:00
    Joseph, B, 3, 15:05:00


    Output:



    Name, Channel, Duration, Start_time
    John, A, 2, 16:00:00
    John, A, 2, 16:01:00
    Joseph, B, 3, 15:05:00
    Joseph, B, 3, 15:06:00
    Joseph, B, 3, 15:07:00


    Thank you in advance.



    enter image description here










    share|improve this question



























      1












      1








      1








      I am facing an issue to write a code using datetime. I had created a scenario I am working on. Can someone help me out on the code.



      Input:



      Name, Channel, Duration, Start_time
      John, A, 2, 16:00:00
      Joseph, B, 3, 15:05:00


      Output:



      Name, Channel, Duration, Start_time
      John, A, 2, 16:00:00
      John, A, 2, 16:01:00
      Joseph, B, 3, 15:05:00
      Joseph, B, 3, 15:06:00
      Joseph, B, 3, 15:07:00


      Thank you in advance.



      enter image description here










      share|improve this question
















      I am facing an issue to write a code using datetime. I had created a scenario I am working on. Can someone help me out on the code.



      Input:



      Name, Channel, Duration, Start_time
      John, A, 2, 16:00:00
      Joseph, B, 3, 15:05:00


      Output:



      Name, Channel, Duration, Start_time
      John, A, 2, 16:00:00
      John, A, 2, 16:01:00
      Joseph, B, 3, 15:05:00
      Joseph, B, 3, 15:06:00
      Joseph, B, 3, 15:07:00


      Thank you in advance.



      enter image description here







      python pandas datetime timedelta






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 6:40









      Vivek Kalyanarangan

      5,0361827




      5,0361827










      asked Nov 19 '18 at 6:39









      Srikanth AyithySrikanth Ayithy

      83




      83
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Use:



          df['Start_time'] = pd.to_timedelta(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)

          print (df)
          Name Channel Duration Start_time
          0 John A 2 16:00:00
          1 John A 2 16:01:00
          2 Joseph B 3 15:05:00
          3 Joseph B 3 15:06:00
          4 Joseph B 3 15:07:00


          Explanation:




          1. Firt convert column Start_time to_timedelta

          2. Then repeat values of index by column Duration and repeat rows by loc

          3. Create counter by cumcount per index values and convert it to 1 minute timedeltas, which are added to new repeated column Start_time

          4. Last reset_index with parameter drop=True for avoid duplicated index values


          EDIT:



          If want datetimes in output solution is same, only first convert values to_datetime:



          df['Start_time'] = pd.to_datetime(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)
          print (df)
          Name Channel Duration Start_time
          0 John A 2 2018-11-19 16:00:00
          1 John A 2 2018-11-19 16:01:00
          2 Joseph B 3 2018-11-19 15:05:00
          3 Joseph B 3 2018-11-19 15:06:00
          4 Joseph B 3 2018-11-19 15:07:00





          share|improve this answer


























          • Brilliant. This worked like a stunner. Thank you very much man..... :)

            – Srikanth Ayithy
            Nov 19 '18 at 9:23











          • @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

            – jezrael
            Nov 19 '18 at 9:24











          • Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

            – Srikanth Ayithy
            Nov 19 '18 at 10:02













          • @SrikanthAyithy - Only one answer should be accepting, can you check it?

            – jezrael
            Nov 19 '18 at 10:15











          • Done! I accepted your answer.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21



















          0














          Use -



          df['dates'] = df.apply(lambda x: list(pd.date_range(start=x['Start_time'], periods=x['Duration'], freq='1min')), axis=1)
          df.set_index(['Name','Channel','Duration', 'Start_time'])['dates'].apply(pd.Series).stack().reset_index().drop(['level_4','Start_time'],1).rename(columns={0:'Start_time'})


          Output



              Name    Channel Duration    Start_time
          0 John A 3 2018-11-19 16:00:00
          1 John A 3 2018-11-19 16:01:00
          2 John A 3 2018-11-19 16:02:00
          3 Joseph B 4 2018-11-19 15:05:00
          4 Joseph B 4 2018-11-19 15:06:00
          5 Joseph B 4 2018-11-19 15:07:00
          6 Joseph B 4 2018-11-19 15:08:00


          Explanation




          1. Apply pd.date_range() to Start_time and Duration

          2. explode that into a df with the second line






          share|improve this answer
























          • This is working too. However, results are going into one row.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21











          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%2f53369488%2fcreating-rows-as-per-duration-using-datetime-pandas%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Use:



          df['Start_time'] = pd.to_timedelta(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)

          print (df)
          Name Channel Duration Start_time
          0 John A 2 16:00:00
          1 John A 2 16:01:00
          2 Joseph B 3 15:05:00
          3 Joseph B 3 15:06:00
          4 Joseph B 3 15:07:00


          Explanation:




          1. Firt convert column Start_time to_timedelta

          2. Then repeat values of index by column Duration and repeat rows by loc

          3. Create counter by cumcount per index values and convert it to 1 minute timedeltas, which are added to new repeated column Start_time

          4. Last reset_index with parameter drop=True for avoid duplicated index values


          EDIT:



          If want datetimes in output solution is same, only first convert values to_datetime:



          df['Start_time'] = pd.to_datetime(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)
          print (df)
          Name Channel Duration Start_time
          0 John A 2 2018-11-19 16:00:00
          1 John A 2 2018-11-19 16:01:00
          2 Joseph B 3 2018-11-19 15:05:00
          3 Joseph B 3 2018-11-19 15:06:00
          4 Joseph B 3 2018-11-19 15:07:00





          share|improve this answer


























          • Brilliant. This worked like a stunner. Thank you very much man..... :)

            – Srikanth Ayithy
            Nov 19 '18 at 9:23











          • @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

            – jezrael
            Nov 19 '18 at 9:24











          • Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

            – Srikanth Ayithy
            Nov 19 '18 at 10:02













          • @SrikanthAyithy - Only one answer should be accepting, can you check it?

            – jezrael
            Nov 19 '18 at 10:15











          • Done! I accepted your answer.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21
















          0














          Use:



          df['Start_time'] = pd.to_timedelta(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)

          print (df)
          Name Channel Duration Start_time
          0 John A 2 16:00:00
          1 John A 2 16:01:00
          2 Joseph B 3 15:05:00
          3 Joseph B 3 15:06:00
          4 Joseph B 3 15:07:00


          Explanation:




          1. Firt convert column Start_time to_timedelta

          2. Then repeat values of index by column Duration and repeat rows by loc

          3. Create counter by cumcount per index values and convert it to 1 minute timedeltas, which are added to new repeated column Start_time

          4. Last reset_index with parameter drop=True for avoid duplicated index values


          EDIT:



          If want datetimes in output solution is same, only first convert values to_datetime:



          df['Start_time'] = pd.to_datetime(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)
          print (df)
          Name Channel Duration Start_time
          0 John A 2 2018-11-19 16:00:00
          1 John A 2 2018-11-19 16:01:00
          2 Joseph B 3 2018-11-19 15:05:00
          3 Joseph B 3 2018-11-19 15:06:00
          4 Joseph B 3 2018-11-19 15:07:00





          share|improve this answer


























          • Brilliant. This worked like a stunner. Thank you very much man..... :)

            – Srikanth Ayithy
            Nov 19 '18 at 9:23











          • @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

            – jezrael
            Nov 19 '18 at 9:24











          • Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

            – Srikanth Ayithy
            Nov 19 '18 at 10:02













          • @SrikanthAyithy - Only one answer should be accepting, can you check it?

            – jezrael
            Nov 19 '18 at 10:15











          • Done! I accepted your answer.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21














          0












          0








          0







          Use:



          df['Start_time'] = pd.to_timedelta(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)

          print (df)
          Name Channel Duration Start_time
          0 John A 2 16:00:00
          1 John A 2 16:01:00
          2 Joseph B 3 15:05:00
          3 Joseph B 3 15:06:00
          4 Joseph B 3 15:07:00


          Explanation:




          1. Firt convert column Start_time to_timedelta

          2. Then repeat values of index by column Duration and repeat rows by loc

          3. Create counter by cumcount per index values and convert it to 1 minute timedeltas, which are added to new repeated column Start_time

          4. Last reset_index with parameter drop=True for avoid duplicated index values


          EDIT:



          If want datetimes in output solution is same, only first convert values to_datetime:



          df['Start_time'] = pd.to_datetime(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)
          print (df)
          Name Channel Duration Start_time
          0 John A 2 2018-11-19 16:00:00
          1 John A 2 2018-11-19 16:01:00
          2 Joseph B 3 2018-11-19 15:05:00
          3 Joseph B 3 2018-11-19 15:06:00
          4 Joseph B 3 2018-11-19 15:07:00





          share|improve this answer















          Use:



          df['Start_time'] = pd.to_timedelta(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)

          print (df)
          Name Channel Duration Start_time
          0 John A 2 16:00:00
          1 John A 2 16:01:00
          2 Joseph B 3 15:05:00
          3 Joseph B 3 15:06:00
          4 Joseph B 3 15:07:00


          Explanation:




          1. Firt convert column Start_time to_timedelta

          2. Then repeat values of index by column Duration and repeat rows by loc

          3. Create counter by cumcount per index values and convert it to 1 minute timedeltas, which are added to new repeated column Start_time

          4. Last reset_index with parameter drop=True for avoid duplicated index values


          EDIT:



          If want datetimes in output solution is same, only first convert values to_datetime:



          df['Start_time'] = pd.to_datetime(df['Start_time'])
          df = df.loc[df.index.repeat(df['Duration'])]
          td = pd.to_timedelta(df.groupby(level=0).cumcount() * 60, unit='s')

          df['Start_time'] = df['Start_time'] + td
          df = df.reset_index(drop=True)
          print (df)
          Name Channel Duration Start_time
          0 John A 2 2018-11-19 16:00:00
          1 John A 2 2018-11-19 16:01:00
          2 Joseph B 3 2018-11-19 15:05:00
          3 Joseph B 3 2018-11-19 15:06:00
          4 Joseph B 3 2018-11-19 15:07:00






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 7:16

























          answered Nov 19 '18 at 6:42









          jezraeljezrael

          329k23271350




          329k23271350













          • Brilliant. This worked like a stunner. Thank you very much man..... :)

            – Srikanth Ayithy
            Nov 19 '18 at 9:23











          • @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

            – jezrael
            Nov 19 '18 at 9:24











          • Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

            – Srikanth Ayithy
            Nov 19 '18 at 10:02













          • @SrikanthAyithy - Only one answer should be accepting, can you check it?

            – jezrael
            Nov 19 '18 at 10:15











          • Done! I accepted your answer.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21



















          • Brilliant. This worked like a stunner. Thank you very much man..... :)

            – Srikanth Ayithy
            Nov 19 '18 at 9:23











          • @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

            – jezrael
            Nov 19 '18 at 9:24











          • Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

            – Srikanth Ayithy
            Nov 19 '18 at 10:02













          • @SrikanthAyithy - Only one answer should be accepting, can you check it?

            – jezrael
            Nov 19 '18 at 10:15











          • Done! I accepted your answer.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21

















          Brilliant. This worked like a stunner. Thank you very much man..... :)

          – Srikanth Ayithy
          Nov 19 '18 at 9:23





          Brilliant. This worked like a stunner. Thank you very much man..... :)

          – Srikanth Ayithy
          Nov 19 '18 at 9:23













          @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

          – jezrael
          Nov 19 '18 at 9:24





          @SrikanthAyithy - You are welcome! If my answer was helpful, don't forget accept it. Thanks.

          – jezrael
          Nov 19 '18 at 9:24













          Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

          – Srikanth Ayithy
          Nov 19 '18 at 10:02







          Thank you very much for the previous answer. Need one more small help. If I have to summarize the input data i mentioned into half an hour bands. Like my output looks-like, Name, Channel, Duration, Timeband, Duration, count John, A, 2, 16:00:00-16:30:00 1 Joseph, B, 3, 15:00:00-15:30:00 1................... We may face an issue when we summarize a row with duration 10 watching which will split between bands like 5 minutes in 15:00:00-15:30:00 and other 5 minutes in 15:30:00-16:00:00.

          – Srikanth Ayithy
          Nov 19 '18 at 10:02















          @SrikanthAyithy - Only one answer should be accepting, can you check it?

          – jezrael
          Nov 19 '18 at 10:15





          @SrikanthAyithy - Only one answer should be accepting, can you check it?

          – jezrael
          Nov 19 '18 at 10:15













          Done! I accepted your answer.

          – Srikanth Ayithy
          Nov 19 '18 at 10:21





          Done! I accepted your answer.

          – Srikanth Ayithy
          Nov 19 '18 at 10:21













          0














          Use -



          df['dates'] = df.apply(lambda x: list(pd.date_range(start=x['Start_time'], periods=x['Duration'], freq='1min')), axis=1)
          df.set_index(['Name','Channel','Duration', 'Start_time'])['dates'].apply(pd.Series).stack().reset_index().drop(['level_4','Start_time'],1).rename(columns={0:'Start_time'})


          Output



              Name    Channel Duration    Start_time
          0 John A 3 2018-11-19 16:00:00
          1 John A 3 2018-11-19 16:01:00
          2 John A 3 2018-11-19 16:02:00
          3 Joseph B 4 2018-11-19 15:05:00
          4 Joseph B 4 2018-11-19 15:06:00
          5 Joseph B 4 2018-11-19 15:07:00
          6 Joseph B 4 2018-11-19 15:08:00


          Explanation




          1. Apply pd.date_range() to Start_time and Duration

          2. explode that into a df with the second line






          share|improve this answer
























          • This is working too. However, results are going into one row.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21
















          0














          Use -



          df['dates'] = df.apply(lambda x: list(pd.date_range(start=x['Start_time'], periods=x['Duration'], freq='1min')), axis=1)
          df.set_index(['Name','Channel','Duration', 'Start_time'])['dates'].apply(pd.Series).stack().reset_index().drop(['level_4','Start_time'],1).rename(columns={0:'Start_time'})


          Output



              Name    Channel Duration    Start_time
          0 John A 3 2018-11-19 16:00:00
          1 John A 3 2018-11-19 16:01:00
          2 John A 3 2018-11-19 16:02:00
          3 Joseph B 4 2018-11-19 15:05:00
          4 Joseph B 4 2018-11-19 15:06:00
          5 Joseph B 4 2018-11-19 15:07:00
          6 Joseph B 4 2018-11-19 15:08:00


          Explanation




          1. Apply pd.date_range() to Start_time and Duration

          2. explode that into a df with the second line






          share|improve this answer
























          • This is working too. However, results are going into one row.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21














          0












          0








          0







          Use -



          df['dates'] = df.apply(lambda x: list(pd.date_range(start=x['Start_time'], periods=x['Duration'], freq='1min')), axis=1)
          df.set_index(['Name','Channel','Duration', 'Start_time'])['dates'].apply(pd.Series).stack().reset_index().drop(['level_4','Start_time'],1).rename(columns={0:'Start_time'})


          Output



              Name    Channel Duration    Start_time
          0 John A 3 2018-11-19 16:00:00
          1 John A 3 2018-11-19 16:01:00
          2 John A 3 2018-11-19 16:02:00
          3 Joseph B 4 2018-11-19 15:05:00
          4 Joseph B 4 2018-11-19 15:06:00
          5 Joseph B 4 2018-11-19 15:07:00
          6 Joseph B 4 2018-11-19 15:08:00


          Explanation




          1. Apply pd.date_range() to Start_time and Duration

          2. explode that into a df with the second line






          share|improve this answer













          Use -



          df['dates'] = df.apply(lambda x: list(pd.date_range(start=x['Start_time'], periods=x['Duration'], freq='1min')), axis=1)
          df.set_index(['Name','Channel','Duration', 'Start_time'])['dates'].apply(pd.Series).stack().reset_index().drop(['level_4','Start_time'],1).rename(columns={0:'Start_time'})


          Output



              Name    Channel Duration    Start_time
          0 John A 3 2018-11-19 16:00:00
          1 John A 3 2018-11-19 16:01:00
          2 John A 3 2018-11-19 16:02:00
          3 Joseph B 4 2018-11-19 15:05:00
          4 Joseph B 4 2018-11-19 15:06:00
          5 Joseph B 4 2018-11-19 15:07:00
          6 Joseph B 4 2018-11-19 15:08:00


          Explanation




          1. Apply pd.date_range() to Start_time and Duration

          2. explode that into a df with the second line







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 7:11









          Vivek KalyanaranganVivek Kalyanarangan

          5,0361827




          5,0361827













          • This is working too. However, results are going into one row.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21



















          • This is working too. However, results are going into one row.

            – Srikanth Ayithy
            Nov 19 '18 at 10:21

















          This is working too. However, results are going into one row.

          – Srikanth Ayithy
          Nov 19 '18 at 10:21





          This is working too. However, results are going into one row.

          – Srikanth Ayithy
          Nov 19 '18 at 10:21


















          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%2f53369488%2fcreating-rows-as-per-duration-using-datetime-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

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word