Dataframe groupby aggregation for percentile









up vote
0
down vote

favorite












Suppose I have a dataframe with columns as:
JobTitle, Age, Stats and Date. Goal is to group it by JobTitle and Age and apply the Aggregation functions to Stats and Date. Stats column will be read from a config file. If it is empty, the default will be 'mean', else it will take the user defined number for percentile.



This is what I have done:



import pandas as pd
import numpy as np
aggregate_dict='Stats':'Mean', 'Date':'min'

for i in range(0,df.shape[0]):
if df.Stats[i]:
temp_StatsName = df.Stats[i]
aggregate_dict='Stats':'percentile('+temp_StatsName+')', 'Date':'min'


df_final=df.groupby(['JobTitle','Age']).agg(aggregate_dict).reset_index()


Besides this, I have also tried creating my own percentile function as so and used it in the aggregate_dict definition but was not successful:



def percentile(n):
def percentile_(x):
return np.percentile(x, n)
percentile_.__name__ = 'percentile_%s' % n
return percentile_


If you may have a suggestion how to implement user defined percentiles when the Stats column is not empty, that would be very helpful. My code may not be ideal, I am relatively new in Python. Thanks!










share|improve this question



























    up vote
    0
    down vote

    favorite












    Suppose I have a dataframe with columns as:
    JobTitle, Age, Stats and Date. Goal is to group it by JobTitle and Age and apply the Aggregation functions to Stats and Date. Stats column will be read from a config file. If it is empty, the default will be 'mean', else it will take the user defined number for percentile.



    This is what I have done:



    import pandas as pd
    import numpy as np
    aggregate_dict='Stats':'Mean', 'Date':'min'

    for i in range(0,df.shape[0]):
    if df.Stats[i]:
    temp_StatsName = df.Stats[i]
    aggregate_dict='Stats':'percentile('+temp_StatsName+')', 'Date':'min'


    df_final=df.groupby(['JobTitle','Age']).agg(aggregate_dict).reset_index()


    Besides this, I have also tried creating my own percentile function as so and used it in the aggregate_dict definition but was not successful:



    def percentile(n):
    def percentile_(x):
    return np.percentile(x, n)
    percentile_.__name__ = 'percentile_%s' % n
    return percentile_


    If you may have a suggestion how to implement user defined percentiles when the Stats column is not empty, that would be very helpful. My code may not be ideal, I am relatively new in Python. Thanks!










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Suppose I have a dataframe with columns as:
      JobTitle, Age, Stats and Date. Goal is to group it by JobTitle and Age and apply the Aggregation functions to Stats and Date. Stats column will be read from a config file. If it is empty, the default will be 'mean', else it will take the user defined number for percentile.



      This is what I have done:



      import pandas as pd
      import numpy as np
      aggregate_dict='Stats':'Mean', 'Date':'min'

      for i in range(0,df.shape[0]):
      if df.Stats[i]:
      temp_StatsName = df.Stats[i]
      aggregate_dict='Stats':'percentile('+temp_StatsName+')', 'Date':'min'


      df_final=df.groupby(['JobTitle','Age']).agg(aggregate_dict).reset_index()


      Besides this, I have also tried creating my own percentile function as so and used it in the aggregate_dict definition but was not successful:



      def percentile(n):
      def percentile_(x):
      return np.percentile(x, n)
      percentile_.__name__ = 'percentile_%s' % n
      return percentile_


      If you may have a suggestion how to implement user defined percentiles when the Stats column is not empty, that would be very helpful. My code may not be ideal, I am relatively new in Python. Thanks!










      share|improve this question















      Suppose I have a dataframe with columns as:
      JobTitle, Age, Stats and Date. Goal is to group it by JobTitle and Age and apply the Aggregation functions to Stats and Date. Stats column will be read from a config file. If it is empty, the default will be 'mean', else it will take the user defined number for percentile.



      This is what I have done:



      import pandas as pd
      import numpy as np
      aggregate_dict='Stats':'Mean', 'Date':'min'

      for i in range(0,df.shape[0]):
      if df.Stats[i]:
      temp_StatsName = df.Stats[i]
      aggregate_dict='Stats':'percentile('+temp_StatsName+')', 'Date':'min'


      df_final=df.groupby(['JobTitle','Age']).agg(aggregate_dict).reset_index()


      Besides this, I have also tried creating my own percentile function as so and used it in the aggregate_dict definition but was not successful:



      def percentile(n):
      def percentile_(x):
      return np.percentile(x, n)
      percentile_.__name__ = 'percentile_%s' % n
      return percentile_


      If you may have a suggestion how to implement user defined percentiles when the Stats column is not empty, that would be very helpful. My code may not be ideal, I am relatively new in Python. Thanks!







      python pandas aggregate pandas-groupby






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 0:20

























      asked Nov 10 at 20:54









      shaucha

      46111




      46111



























          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%2f53243319%2fdataframe-groupby-aggregation-for-percentile%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















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243319%2fdataframe-groupby-aggregation-for-percentile%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

          27

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          Category:Rhetoric