Identifying which Time-series column grew within a pandas DataFrame based on boolean conditions












0














1) My Pandas dataframe consists of a panel of timeseries (C1,C2,C3.



2)The time-series are indexed from most recent period "5" to 4 days ago "0".



3) I want to create a column that identifies only rows were only 1 time-series had a unique maximum. Once this row is identified that time-series will be compared to its two previous lags to see if it's current value is larger than it's lagged values (i.e. shift(-1) and shift(-2)). Also for growth to have occurred shift(-1) > shift(-2).



4) For example,Gwth_Col (Growth Column) is 1 (C1) for the first value. C1 is the largest value on the current row and it is larger than its 1 period lag and it's 1 period lag is larger than it's 2 period lag.



I understand pieces of the puzzle, thanks to Stackoverflow contributors, but I can't gain traction on pulling it together to crate a flexible function or apply function to calculate this new column. Thanks for any help, much appreciate it!



# Find out which unique exhibited growth in the time series.
# create a dictionary
dict = {
'C1': [9,7,6,6,7,8],
'C2': [8,8,7,6,5,6],
'C3': [5,5,3,2,1,7],
'Gwth_Col':[1,2,2,0,0,0]
}

# create a list of strings
columns = ['C1', 'C2','C3','Gwth_Col']

index = [5,4,3,2,1,0]

# Passing a dictionary
# key: column name
# value: series of values
df = pd.DataFrame(dict, columns=columns, index=index)
df


Data Frame Results










share|improve this question



























    0














    1) My Pandas dataframe consists of a panel of timeseries (C1,C2,C3.



    2)The time-series are indexed from most recent period "5" to 4 days ago "0".



    3) I want to create a column that identifies only rows were only 1 time-series had a unique maximum. Once this row is identified that time-series will be compared to its two previous lags to see if it's current value is larger than it's lagged values (i.e. shift(-1) and shift(-2)). Also for growth to have occurred shift(-1) > shift(-2).



    4) For example,Gwth_Col (Growth Column) is 1 (C1) for the first value. C1 is the largest value on the current row and it is larger than its 1 period lag and it's 1 period lag is larger than it's 2 period lag.



    I understand pieces of the puzzle, thanks to Stackoverflow contributors, but I can't gain traction on pulling it together to crate a flexible function or apply function to calculate this new column. Thanks for any help, much appreciate it!



    # Find out which unique exhibited growth in the time series.
    # create a dictionary
    dict = {
    'C1': [9,7,6,6,7,8],
    'C2': [8,8,7,6,5,6],
    'C3': [5,5,3,2,1,7],
    'Gwth_Col':[1,2,2,0,0,0]
    }

    # create a list of strings
    columns = ['C1', 'C2','C3','Gwth_Col']

    index = [5,4,3,2,1,0]

    # Passing a dictionary
    # key: column name
    # value: series of values
    df = pd.DataFrame(dict, columns=columns, index=index)
    df


    Data Frame Results










    share|improve this question

























      0












      0








      0







      1) My Pandas dataframe consists of a panel of timeseries (C1,C2,C3.



      2)The time-series are indexed from most recent period "5" to 4 days ago "0".



      3) I want to create a column that identifies only rows were only 1 time-series had a unique maximum. Once this row is identified that time-series will be compared to its two previous lags to see if it's current value is larger than it's lagged values (i.e. shift(-1) and shift(-2)). Also for growth to have occurred shift(-1) > shift(-2).



      4) For example,Gwth_Col (Growth Column) is 1 (C1) for the first value. C1 is the largest value on the current row and it is larger than its 1 period lag and it's 1 period lag is larger than it's 2 period lag.



      I understand pieces of the puzzle, thanks to Stackoverflow contributors, but I can't gain traction on pulling it together to crate a flexible function or apply function to calculate this new column. Thanks for any help, much appreciate it!



      # Find out which unique exhibited growth in the time series.
      # create a dictionary
      dict = {
      'C1': [9,7,6,6,7,8],
      'C2': [8,8,7,6,5,6],
      'C3': [5,5,3,2,1,7],
      'Gwth_Col':[1,2,2,0,0,0]
      }

      # create a list of strings
      columns = ['C1', 'C2','C3','Gwth_Col']

      index = [5,4,3,2,1,0]

      # Passing a dictionary
      # key: column name
      # value: series of values
      df = pd.DataFrame(dict, columns=columns, index=index)
      df


      Data Frame Results










      share|improve this question













      1) My Pandas dataframe consists of a panel of timeseries (C1,C2,C3.



      2)The time-series are indexed from most recent period "5" to 4 days ago "0".



      3) I want to create a column that identifies only rows were only 1 time-series had a unique maximum. Once this row is identified that time-series will be compared to its two previous lags to see if it's current value is larger than it's lagged values (i.e. shift(-1) and shift(-2)). Also for growth to have occurred shift(-1) > shift(-2).



      4) For example,Gwth_Col (Growth Column) is 1 (C1) for the first value. C1 is the largest value on the current row and it is larger than its 1 period lag and it's 1 period lag is larger than it's 2 period lag.



      I understand pieces of the puzzle, thanks to Stackoverflow contributors, but I can't gain traction on pulling it together to crate a flexible function or apply function to calculate this new column. Thanks for any help, much appreciate it!



      # Find out which unique exhibited growth in the time series.
      # create a dictionary
      dict = {
      'C1': [9,7,6,6,7,8],
      'C2': [8,8,7,6,5,6],
      'C3': [5,5,3,2,1,7],
      'Gwth_Col':[1,2,2,0,0,0]
      }

      # create a list of strings
      columns = ['C1', 'C2','C3','Gwth_Col']

      index = [5,4,3,2,1,0]

      # Passing a dictionary
      # key: column name
      # value: series of values
      df = pd.DataFrame(dict, columns=columns, index=index)
      df


      Data Frame Results







      python pandas time-series list-comprehension boolean-logic






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 22:23









      rer49rer49

      415




      415
























          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%2f53270960%2fidentifying-which-time-series-column-grew-within-a-pandas-dataframe-based-on-boo%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%2f53270960%2fidentifying-which-time-series-column-grew-within-a-pandas-dataframe-based-on-boo%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

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values