How to apply resample on data frame column containing array












0















I have a pandas dataframe with columns run, type, vectime and vecvalue.
For rows (probably not the correct term here) with type=="vector" the vectime and vecvalue contains arrays:



loaded_csv.tail(2)

run type module name attrname attrvalue value vectime vecvalue
709 run-id1 vector client499.app[0] packetReceived:vector(packetBytes) NaN None NaN [20.02926688, 20.04433504, 20.04556544, 20.059... [1460.0, 1460.0, 1460.0, 1460.0, 1460.0, 1460....
710 run-id1 attr client499.app[0] packetReceived:vector(packetBytes) checkSignals False NaN None None


The 'vectime' contains time points for the values in 'vecvalue'.



I have manged to run simple things such as apply min() max() to the array and assigning the value to the original dataframe by running:



loaded_csv.loc[(loaded_csv.type == "vector"),('min_vectime')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime')].apply(min)


Which in my understanding 'filters' rows with type == vector and adds new column 'min_vectime' with the appropriate value.



However I am unable to figure out how to do similar thing with resample on 'vecvalue' using 'vectime' as index and replacing the original values (with the resampled ones) or adding new columns.



I found a way how to achieve part of it by getting the data from the dataframe, create Series and apply the resample.



for row in loaded_csv[loaded_csv.type == "vector"].itertuples():
print_series = pd.Series(row.vecvalue, index = pd.to_timedelta(row.vectime, unit='s'))
result = print_series.resample('10ms',label='right').sum()*8
resultPlot = result.rolling( window=30, min_periods=10).mean()


I am looking for a cleaner and less complicated way.



There are many examples on each of these tasks - slicing dataframe, applying function whether using .resample directly or via .apply() but on a simpler/different dataframe structure and I get lost somewhere in between.



Partial solution



I found a way how to turn the arrays into Series and resample it but when I try to insert back to the dataframe as a new column I get only NaN.
I had wrap it in pd.Dataframe() to get it included in the resulting Dataframe.
But now I have a column with Dataframe [Series] instead of just Series.



loaded_csv.loc[(loaded_csv.type == "vector"),('resampled')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime', 'vecvalue')].apply(lambda x:  pd.DataFrame(pd.Series(x.vecvalue, index = (pd.to_timedelta(x.vectime, unit='s')))), axis= 1)
loaded_csv.loc[(loaded_csv3.type == "vector"),('resampled')].head()

76 0
00:00:20.029266 1460....
157 0
Name: resampled, dtype: object









share|improve this question





























    0















    I have a pandas dataframe with columns run, type, vectime and vecvalue.
    For rows (probably not the correct term here) with type=="vector" the vectime and vecvalue contains arrays:



    loaded_csv.tail(2)

    run type module name attrname attrvalue value vectime vecvalue
    709 run-id1 vector client499.app[0] packetReceived:vector(packetBytes) NaN None NaN [20.02926688, 20.04433504, 20.04556544, 20.059... [1460.0, 1460.0, 1460.0, 1460.0, 1460.0, 1460....
    710 run-id1 attr client499.app[0] packetReceived:vector(packetBytes) checkSignals False NaN None None


    The 'vectime' contains time points for the values in 'vecvalue'.



    I have manged to run simple things such as apply min() max() to the array and assigning the value to the original dataframe by running:



    loaded_csv.loc[(loaded_csv.type == "vector"),('min_vectime')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime')].apply(min)


    Which in my understanding 'filters' rows with type == vector and adds new column 'min_vectime' with the appropriate value.



    However I am unable to figure out how to do similar thing with resample on 'vecvalue' using 'vectime' as index and replacing the original values (with the resampled ones) or adding new columns.



    I found a way how to achieve part of it by getting the data from the dataframe, create Series and apply the resample.



    for row in loaded_csv[loaded_csv.type == "vector"].itertuples():
    print_series = pd.Series(row.vecvalue, index = pd.to_timedelta(row.vectime, unit='s'))
    result = print_series.resample('10ms',label='right').sum()*8
    resultPlot = result.rolling( window=30, min_periods=10).mean()


    I am looking for a cleaner and less complicated way.



    There are many examples on each of these tasks - slicing dataframe, applying function whether using .resample directly or via .apply() but on a simpler/different dataframe structure and I get lost somewhere in between.



    Partial solution



    I found a way how to turn the arrays into Series and resample it but when I try to insert back to the dataframe as a new column I get only NaN.
    I had wrap it in pd.Dataframe() to get it included in the resulting Dataframe.
    But now I have a column with Dataframe [Series] instead of just Series.



    loaded_csv.loc[(loaded_csv.type == "vector"),('resampled')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime', 'vecvalue')].apply(lambda x:  pd.DataFrame(pd.Series(x.vecvalue, index = (pd.to_timedelta(x.vectime, unit='s')))), axis= 1)
    loaded_csv.loc[(loaded_csv3.type == "vector"),('resampled')].head()

    76 0
    00:00:20.029266 1460....
    157 0
    Name: resampled, dtype: object









    share|improve this question



























      0












      0








      0








      I have a pandas dataframe with columns run, type, vectime and vecvalue.
      For rows (probably not the correct term here) with type=="vector" the vectime and vecvalue contains arrays:



      loaded_csv.tail(2)

      run type module name attrname attrvalue value vectime vecvalue
      709 run-id1 vector client499.app[0] packetReceived:vector(packetBytes) NaN None NaN [20.02926688, 20.04433504, 20.04556544, 20.059... [1460.0, 1460.0, 1460.0, 1460.0, 1460.0, 1460....
      710 run-id1 attr client499.app[0] packetReceived:vector(packetBytes) checkSignals False NaN None None


      The 'vectime' contains time points for the values in 'vecvalue'.



      I have manged to run simple things such as apply min() max() to the array and assigning the value to the original dataframe by running:



      loaded_csv.loc[(loaded_csv.type == "vector"),('min_vectime')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime')].apply(min)


      Which in my understanding 'filters' rows with type == vector and adds new column 'min_vectime' with the appropriate value.



      However I am unable to figure out how to do similar thing with resample on 'vecvalue' using 'vectime' as index and replacing the original values (with the resampled ones) or adding new columns.



      I found a way how to achieve part of it by getting the data from the dataframe, create Series and apply the resample.



      for row in loaded_csv[loaded_csv.type == "vector"].itertuples():
      print_series = pd.Series(row.vecvalue, index = pd.to_timedelta(row.vectime, unit='s'))
      result = print_series.resample('10ms',label='right').sum()*8
      resultPlot = result.rolling( window=30, min_periods=10).mean()


      I am looking for a cleaner and less complicated way.



      There are many examples on each of these tasks - slicing dataframe, applying function whether using .resample directly or via .apply() but on a simpler/different dataframe structure and I get lost somewhere in between.



      Partial solution



      I found a way how to turn the arrays into Series and resample it but when I try to insert back to the dataframe as a new column I get only NaN.
      I had wrap it in pd.Dataframe() to get it included in the resulting Dataframe.
      But now I have a column with Dataframe [Series] instead of just Series.



      loaded_csv.loc[(loaded_csv.type == "vector"),('resampled')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime', 'vecvalue')].apply(lambda x:  pd.DataFrame(pd.Series(x.vecvalue, index = (pd.to_timedelta(x.vectime, unit='s')))), axis= 1)
      loaded_csv.loc[(loaded_csv3.type == "vector"),('resampled')].head()

      76 0
      00:00:20.029266 1460....
      157 0
      Name: resampled, dtype: object









      share|improve this question
















      I have a pandas dataframe with columns run, type, vectime and vecvalue.
      For rows (probably not the correct term here) with type=="vector" the vectime and vecvalue contains arrays:



      loaded_csv.tail(2)

      run type module name attrname attrvalue value vectime vecvalue
      709 run-id1 vector client499.app[0] packetReceived:vector(packetBytes) NaN None NaN [20.02926688, 20.04433504, 20.04556544, 20.059... [1460.0, 1460.0, 1460.0, 1460.0, 1460.0, 1460....
      710 run-id1 attr client499.app[0] packetReceived:vector(packetBytes) checkSignals False NaN None None


      The 'vectime' contains time points for the values in 'vecvalue'.



      I have manged to run simple things such as apply min() max() to the array and assigning the value to the original dataframe by running:



      loaded_csv.loc[(loaded_csv.type == "vector"),('min_vectime')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime')].apply(min)


      Which in my understanding 'filters' rows with type == vector and adds new column 'min_vectime' with the appropriate value.



      However I am unable to figure out how to do similar thing with resample on 'vecvalue' using 'vectime' as index and replacing the original values (with the resampled ones) or adding new columns.



      I found a way how to achieve part of it by getting the data from the dataframe, create Series and apply the resample.



      for row in loaded_csv[loaded_csv.type == "vector"].itertuples():
      print_series = pd.Series(row.vecvalue, index = pd.to_timedelta(row.vectime, unit='s'))
      result = print_series.resample('10ms',label='right').sum()*8
      resultPlot = result.rolling( window=30, min_periods=10).mean()


      I am looking for a cleaner and less complicated way.



      There are many examples on each of these tasks - slicing dataframe, applying function whether using .resample directly or via .apply() but on a simpler/different dataframe structure and I get lost somewhere in between.



      Partial solution



      I found a way how to turn the arrays into Series and resample it but when I try to insert back to the dataframe as a new column I get only NaN.
      I had wrap it in pd.Dataframe() to get it included in the resulting Dataframe.
      But now I have a column with Dataframe [Series] instead of just Series.



      loaded_csv.loc[(loaded_csv.type == "vector"),('resampled')] = loaded_csv.loc[(loaded_csv.type == "vector"),('vectime', 'vecvalue')].apply(lambda x:  pd.DataFrame(pd.Series(x.vecvalue, index = (pd.to_timedelta(x.vectime, unit='s')))), axis= 1)
      loaded_csv.loc[(loaded_csv3.type == "vector"),('resampled')].head()

      76 0
      00:00:20.029266 1460....
      157 0
      Name: resampled, dtype: object






      python pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 21:24







      badluck

















      asked Nov 21 '18 at 17:35









      badluckbadluck

      12




      12
























          0






          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%2f53417705%2fhow-to-apply-resample-on-data-frame-column-containing-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417705%2fhow-to-apply-resample-on-data-frame-column-containing-array%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 change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?