find maximum value in col C in pandas dataframe while group by both col A and B











up vote
0
down vote

favorite












I have a pandas dataframe like this:



df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'],'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95]})

RT Quality Name Similarity
0 9 70 a 0.98
1 10 60 a 0.97
2 10 50 b 0.97
3 11 60 c 0.95
4 11 80 b 0.95
5 11 70 c 0.95
6 11 80 b 0.95


The values in the column Similarity has the same group-by with column RT



I want to group column RT and find the maximum column Quality value and group by column Name.



For example:



In column RT value 11,which have column Name value c and b, sum each of the column Quality values, then get c = 130, b =160, and sort the maximum 160, b
then get



    RT  Quality Name    Similarity
0 9 70 a 0.98
1 10 60 a 0.97
2 10 50 b 0.97
3 11 160 b 0.95
4 11 130 c 0.95









share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a pandas dataframe like this:



    df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'],'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95]})

    RT Quality Name Similarity
    0 9 70 a 0.98
    1 10 60 a 0.97
    2 10 50 b 0.97
    3 11 60 c 0.95
    4 11 80 b 0.95
    5 11 70 c 0.95
    6 11 80 b 0.95


    The values in the column Similarity has the same group-by with column RT



    I want to group column RT and find the maximum column Quality value and group by column Name.



    For example:



    In column RT value 11,which have column Name value c and b, sum each of the column Quality values, then get c = 130, b =160, and sort the maximum 160, b
    then get



        RT  Quality Name    Similarity
    0 9 70 a 0.98
    1 10 60 a 0.97
    2 10 50 b 0.97
    3 11 160 b 0.95
    4 11 130 c 0.95









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a pandas dataframe like this:



      df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'],'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95]})

      RT Quality Name Similarity
      0 9 70 a 0.98
      1 10 60 a 0.97
      2 10 50 b 0.97
      3 11 60 c 0.95
      4 11 80 b 0.95
      5 11 70 c 0.95
      6 11 80 b 0.95


      The values in the column Similarity has the same group-by with column RT



      I want to group column RT and find the maximum column Quality value and group by column Name.



      For example:



      In column RT value 11,which have column Name value c and b, sum each of the column Quality values, then get c = 130, b =160, and sort the maximum 160, b
      then get



          RT  Quality Name    Similarity
      0 9 70 a 0.98
      1 10 60 a 0.97
      2 10 50 b 0.97
      3 11 160 b 0.95
      4 11 130 c 0.95









      share|improve this question















      I have a pandas dataframe like this:



      df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'],'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95]})

      RT Quality Name Similarity
      0 9 70 a 0.98
      1 10 60 a 0.97
      2 10 50 b 0.97
      3 11 60 c 0.95
      4 11 80 b 0.95
      5 11 70 c 0.95
      6 11 80 b 0.95


      The values in the column Similarity has the same group-by with column RT



      I want to group column RT and find the maximum column Quality value and group by column Name.



      For example:



      In column RT value 11,which have column Name value c and b, sum each of the column Quality values, then get c = 130, b =160, and sort the maximum 160, b
      then get



          RT  Quality Name    Similarity
      0 9 70 a 0.98
      1 10 60 a 0.97
      2 10 50 b 0.97
      3 11 160 b 0.95
      4 11 130 c 0.95






      python pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 7:33









      ssemilla

      2,687423




      2,687423










      asked Nov 20 at 2:27









      X.tang

      93




      93
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          you can use groupby with agg:



          use lambda to return all Similarities or max to return the max



          df.groupby(['RT','Name']).agg({'Quality':'sum', 'Similarity':lambda x:x.unique()})

          Quality Similarity
          RT Name
          9 a 70 0.98
          10 a 60 0.97
          b 50 0.97
          11 b 160 0.95
          c 130 0.95





          share|improve this answer























          • thank you very much~
            – X.tang
            Nov 21 at 11:49


















          up vote
          0
          down vote













          You may not need agg



          df.groupby(['RT','Similarity','Name'],as_index=False)['Quality'].sum()
          Out[150]:
          RT Similarity Name Quality
          0 9 0.98 a 70
          1 10 0.97 a 60
          2 10 0.97 b 50
          3 11 0.95 b 160
          4 11 0.95 c 130





          share|improve this answer





















          • Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
            – X.tang
            Nov 21 at 11:47













          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%2f53385348%2ffind-maximum-value-in-col-c-in-pandas-dataframe-while-group-by-both-col-a-and-b%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








          up vote
          1
          down vote













          you can use groupby with agg:



          use lambda to return all Similarities or max to return the max



          df.groupby(['RT','Name']).agg({'Quality':'sum', 'Similarity':lambda x:x.unique()})

          Quality Similarity
          RT Name
          9 a 70 0.98
          10 a 60 0.97
          b 50 0.97
          11 b 160 0.95
          c 130 0.95





          share|improve this answer























          • thank you very much~
            – X.tang
            Nov 21 at 11:49















          up vote
          1
          down vote













          you can use groupby with agg:



          use lambda to return all Similarities or max to return the max



          df.groupby(['RT','Name']).agg({'Quality':'sum', 'Similarity':lambda x:x.unique()})

          Quality Similarity
          RT Name
          9 a 70 0.98
          10 a 60 0.97
          b 50 0.97
          11 b 160 0.95
          c 130 0.95





          share|improve this answer























          • thank you very much~
            – X.tang
            Nov 21 at 11:49













          up vote
          1
          down vote










          up vote
          1
          down vote









          you can use groupby with agg:



          use lambda to return all Similarities or max to return the max



          df.groupby(['RT','Name']).agg({'Quality':'sum', 'Similarity':lambda x:x.unique()})

          Quality Similarity
          RT Name
          9 a 70 0.98
          10 a 60 0.97
          b 50 0.97
          11 b 160 0.95
          c 130 0.95





          share|improve this answer














          you can use groupby with agg:



          use lambda to return all Similarities or max to return the max



          df.groupby(['RT','Name']).agg({'Quality':'sum', 'Similarity':lambda x:x.unique()})

          Quality Similarity
          RT Name
          9 a 70 0.98
          10 a 60 0.97
          b 50 0.97
          11 b 160 0.95
          c 130 0.95






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 2:56

























          answered Nov 20 at 2:36









          Chris

          1,3731210




          1,3731210












          • thank you very much~
            – X.tang
            Nov 21 at 11:49


















          • thank you very much~
            – X.tang
            Nov 21 at 11:49
















          thank you very much~
          – X.tang
          Nov 21 at 11:49




          thank you very much~
          – X.tang
          Nov 21 at 11:49












          up vote
          0
          down vote













          You may not need agg



          df.groupby(['RT','Similarity','Name'],as_index=False)['Quality'].sum()
          Out[150]:
          RT Similarity Name Quality
          0 9 0.98 a 70
          1 10 0.97 a 60
          2 10 0.97 b 50
          3 11 0.95 b 160
          4 11 0.95 c 130





          share|improve this answer





















          • Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
            – X.tang
            Nov 21 at 11:47

















          up vote
          0
          down vote













          You may not need agg



          df.groupby(['RT','Similarity','Name'],as_index=False)['Quality'].sum()
          Out[150]:
          RT Similarity Name Quality
          0 9 0.98 a 70
          1 10 0.97 a 60
          2 10 0.97 b 50
          3 11 0.95 b 160
          4 11 0.95 c 130





          share|improve this answer





















          • Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
            – X.tang
            Nov 21 at 11:47















          up vote
          0
          down vote










          up vote
          0
          down vote









          You may not need agg



          df.groupby(['RT','Similarity','Name'],as_index=False)['Quality'].sum()
          Out[150]:
          RT Similarity Name Quality
          0 9 0.98 a 70
          1 10 0.97 a 60
          2 10 0.97 b 50
          3 11 0.95 b 160
          4 11 0.95 c 130





          share|improve this answer












          You may not need agg



          df.groupby(['RT','Similarity','Name'],as_index=False)['Quality'].sum()
          Out[150]:
          RT Similarity Name Quality
          0 9 0.98 a 70
          1 10 0.97 a 60
          2 10 0.97 b 50
          3 11 0.95 b 160
          4 11 0.95 c 130






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 4:26









          W-B

          96.3k72962




          96.3k72962












          • Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
            – X.tang
            Nov 21 at 11:47




















          • Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
            – X.tang
            Nov 21 at 11:47


















          Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
          – X.tang
          Nov 21 at 11:47






          Thank you for your reply, what is the difference between agg and as_index? and if I have some irrelevant column,how to keep those column use your method . df = pd.DataFrame({"RT":[9,10,10,11,11,11,11],"Quality":[70,60,50,60,80,70,80],'Name' :['a','a','b','c','b','c','b'], 'Similarity':[0.98,0.97,0.97,0.95,0.95,0.95,0.95],"samples":[13,2,4,5,6,6,7,]}) such as keep the column samples along with column ['RT','Similarity','Name']
          – X.tang
          Nov 21 at 11:47




















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53385348%2ffind-maximum-value-in-col-c-in-pandas-dataframe-while-group-by-both-col-a-and-b%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python