title: Groupby on pandas dataframe and concatenate strings with comma based on the frequency of values in a column
body: <p>This is an update to the structure of my DataFrame, I formulated the structure in haste, I was inspecting a single user and mocked up that structure. @liliscent's remark: "data accidentally satisfies this condition" is also true and value_counts and cum_sum() solves it. But then user_id's also change, and different user's can have the same meet_id if they have the same text.</p>
  
  <p>Updated DataFrames structure:</p>
  
  <pre><code>   mytable = pd.DataFrame({'user_id': [ '3c', '3c', '3c', '3c','3c', '3c', '3c', '3c', '3c', '3c', '3c', '3c', '3c', '3d',
                                   '3d', '3d', '3d', '3e', '3e', '3r', '3w', '3w', '3w', '3w'],
                'meet_id': [1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,4,5,6,1,2,1,1], 'text': ['abc', 'abc', 'abc', 'abc', 'abc', 'abc', 'abc',
          'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'npq', 'npq', 'npq', 'npq', 'tt', 'op', 'li', 'abc', 'xyz', 'abc', 'abc'], 'label': ['A', 'A', 'A', 'A', 'A','B', 'B', 'B', 'B', 'B',
      'C', 'C', 'A', 'G', 'H', 'H', 'H', 'A', 'A', 'B', 'E', 'G', 'B', 'B']})
     mytable =  mytable[['user_id', 'meet_id', 'text', 'label']] # ordering columns in the way I would like to be printed out.
  
     user_id  meet_id  text label
     3c        1      abc     A
     3c        1      abc     A
     3c        1      abc     A
     3c        1      abc     A
     3c        1      abc     A
     3c        1      abc     B
     3c        1      abc     B
     3c        2      xyz     B
     3c        2      xyz     B
     3c        2      xyz     B
     3c        2      xyz     C
     3c        2      xyz     C
     3c        2      xyz     A
     3d        3      npq     G
     3d        3      npq     H
     3d        3      npq     H
     3d        3      npq     H
     3e        4      tt      A
     3e        5      op      A
     3r        6      li      B
     3w        1      abc     E
     3w        2      xyz     G 
     3w        1      abc     B
     3w        1      abc     B
  </code></pre>
  
  <p>I would like to groupby on [user_id &amp; meet_id] column and concatenate the label column in such a way that the label with higher frequency for that group is left untouched, while the second most frequent label will have the first label concatenated, and the last label will have all labels concatenated. </p>
  
  <p>updated DataFrame output is what I am looking for</p>
  
  <pre><code>    mytable_pro = pd.DataFrame({'user_id': ['3c', '3c', '3c', '3c','3c', '3c', '3c', '3c', '3c', '3c', '3c', '3c', '3c','3d',
                                   '3d', '3d', '3d', '3e', '3e', '3r', '3w', '3w', '3w', '3w'],
                'meet_id': [1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,4,5,6,1,2,1,1], 'text': ['abc', 'abc', 'abc', 'abc', 'abc', 'abc', 'abc',
          'xyz', 'xyz', 'xyz', 'xyz', 'xyz', 'xyz','npq', 'npq', 'npq', 'npq', 'tt', 'op', 'li', 'abc', 'xyz', 'abc', 'abc' ], 'label': ['A', 'A', 'A', 'A', 'A', 'B,A', 'B,A', 'B', 'B', 'B',
      'B, C', 'B, C', 'A,B,C', 'H,G', 'H', 'H', 'H', 'A', 'A', 'B', 'E,B', 'G', 'B', 'B']})
      mytable_pro = mytable_pro[['user_id', 'meet_id', 'text', 'label']] # ordering columns in the way I would like to be printed out.
  </code></pre>
  
  <p>This gives:</p>
  
  <pre><code>    user_id  meet_id text  label
     3c        1       abc      A
     3c        1       abc      A
     3c        1       abc      A
     3c        1       abc      A
     3c        1       abc      A
     3c        1       abc     B,A
     3c        1       abc     B,A
     3c        2       xyz      B
     3c        2       xyz      B
     3c        2       xyz      B
     3c        2       xyz    B, C
     3c        2       xyz    B, C
     3c        2       xyz    A,B,C
     3d        3       npq     H,G
     3d        3       npq      H
     3d        3       npq      H
     3d        3       npq      H
     3e        4       tt       A
     3e        5       op       A
     3r        6       li       B
     3w        1       abc     E,B
     3w        2       xyz      G
     3w        1       abc      B
     3w        1       abc      B
  </code></pre>
  
  <p>The answer given by @piRSquared:</p>
  
  <pre><code>    mytable.groupby('meet_id').label.value_counts().groupby('meet_id').apply(
  lambda d: d.index.to_series().str[1].cumsum().str.join(', '))        
  </code></pre>
  
  <p>is the CORRECT ANSWER for the WRONG question I asked, thanks a ton and really sorry. It solves the ordering problem as mentioned previously but would not work if a different user has the same meet_id. Just to be exhaustive, if the label frequency turns out to be equal for a group, it does not matter which of the label gets the other concatenated.</p>
  
  <p>It gives:</p>
  
  <pre><code>     user_id  meet_id  text       label
     3c          1         abc           A
     3c          1         abc           A
     3c          1         abc           A
     3c          1         abc           A
     3c          1         abc           A
     3c          1         abc        A, B
     3c          1         abc        A, B
     3c          2         xyz           B
     3c          2         xyz           B
     3c          2         xyz           B
     3c          2         xyz        B, C
     3c          2         xyz        B, C
     3c          2         xyz     B, C, A
     3d          3         npq        H, G
     3d          3         npq           H
     3d          3         npq           H
     3d          3         npq           H
     3e          4          tt           A
     3e          5          op           A
     3r          6          li           B
     3w          1         abc     A, B, E
     3w          2         xyz    B, C, A, G
     3w          1         abc        A, B
     3w          1         abc        A, B
  </code></pre>
  
  <p>The labels for 3w are off since the labels for meet_id are picked up ignoring the difference is user_id. My bad! </p>
  
  <p>Now, since user_id must also be considered, I tried the following:</p>
  
  <pre><code>    s = mytable.groupby(['user_id', 'meet_id']).label.value_counts().groupby(['user_id, 'meet_id']).apply(
  lambda d: d.index.to_series().str[1].cumsum().str.join(', '))        
  </code></pre>
  
  <p>This throws:</p>
  
  <pre><code>    AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
  </code></pre>
  
  <p>Ah! Another small update, In reality I have words in my label column.</p>
  
  <pre><code>    dummy_boo = pd.DataFrame({'user_id': ['3g', '3g', '3g'], 'meet_id': [9,9,9], 'text': ['baby', 'baby', 'baby'], 'label':['hello', 'hello', 'why']}
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>    user_id  meet_id  text  label
        3g        9     baby  hello
        3g        9     baby  hello
        3g        9     baby   why
  </code></pre>
  
  <p>Applying the above code is resulting in each character being separated by a comma. </p>
  
  <pre><code> user_id  meet_id  text   label
    3g        9      baby  h, e, l, l, o
    3g        9      baby  h, e, l, l, o
    3g        9      baby  h, e, l, l, o, w, h, y
  </code></pre>
  
  <p>Instead I need:</p>
  
  <pre><code>    user_id  meet_id  text   label
    3g        9      baby    hello
    3g        9      baby    hello
    3g        9      baby    hello, why
  </code></pre>
  
  <p>The dtype for label is object. Should we use astype instead. A Big thank you to everyone for helping me out. </p>