title: Is it possible to do applymap using the groupby in pandas?
body: <p>In a pandas Dataframe I want to <code>applymap(somefunction)</code> using <code>groupby</code> (using some column index values).</p>
  
  <p><strong>mcve_01.txt</strong></p>
  
  <pre><code>pos         index      M1      M2      F1_x 
  16230484    141      G/G      G/G       G
  16230491    141      C/C      C/C       C
  16230503    141      T/T      T/T       T
  16230524    141      T/T      T/T       T
  16230535    141      .    .         T
  16232072    211      A/A      A/A       A
  16232072    211      A/A      A/A       A
  16229783    211      C/C      C/C       G
  16229992    211      A/A      A/A       G
  16230007    211      T/T      T/T       A
  16230011    263      G/G      G/G       C
  16230049    263      A/A      A/A       T
  16230174    263      .         .        T
  16230190    263      A/A      A/A       T
  16230260    263      A/A      A/A       G
  </code></pre>
  
  <p>I have function written to do some analyses for columns A, B, C, D
  where the values in A, B, C and D are list.</p>
  
  <pre><code>mcve_data = pd.read_csv('mcve_01.txt', sep='\t')
  
  mcve_data.set_index(['pos', 'index'], append= True, inplace = True)
  mcve_list = mcve_data.applymap(lambda c:[list(c)])
  </code></pre>
  
  <p>say the function is,</p>
  
  <pre><code>def mapfun(c):
  if any(['.' in l for l in c]):
      return '.'
  
  if all(['|' in l for l in c]):
      fun = zip
  
  else:
      fun = product
  
  filt_set = set(['|','/'])
  filt = partial(filter,lambda l: not (l in filt_set))
  
  return ','.join('g'.join(t) for t in fun(*map(filt, c)))
  </code></pre>
  
  <p><strong>Finally:</strong></p>
  
  <pre><code>mcve_mm = (mcve_list+mcve_list.shift(1)).dropna(how='all').\
      applymap(mapfun)
  </code></pre>
  
  <p>which gives me <strong>(final output)</strong>:</p>
  
  <pre><code>pos    index      M1        M2      F1_x    
  16230484    141  CgG,CgG,CgG,CgG        CgG,CgG,CgG,CgG         CgG
  16230491    141  TgC,TgC,TgC,TgC        TgC,TgC,TgC,TgC         TgC
  .....      ...   TgT,TgT,TgT,TgT        TgT,TgT,TgT,TgT         TgT
                 .        .       TgT
                 .        .       AgT
                 AgA,AgA,AgA,AgA          AgA,AgA,AgA,AgA         AgA
                 CgA,CgA,CgA,CgA          CgA,CgA,CgA,CgA         GgA
                 AgC,AgC,AgC,AgC          AgC,AgC,AgC,AgC         GgG
                 TgA,TgA,TgA,TgA          TgA,TgA,TgA,TgA         AgG
                 GgT,GgT,GgT,GgT          GgT,GgT,GgT,GgT         CgA
                 AgG,AgG,AgG,AgG          AgG,AgG,AgG,AgG         TgC
  </code></pre>
  
  <p>So, this code works if I want to run the function (mapfun) for the whole dataframe without grouping. But, i want to run the function by grouping them by <strong>index</strong> values. </p>
  
  <p>Unfortunately, I don't see any example of groupby and applymap together.</p>
  
  <p>I tried then reindexing the index column and then wrap the function (mapfun) within apply, which didn't work.</p>
  
  <pre><code>mcve_mm = (mcve_list+mcve_list.shift(1)).dropna(how='all').groupby(['f1_index'], group_keys = False).apply(lambda x: [mapfun])
  </code></pre>
  
  <p>I didn't get any error but the function part got messed up when trying to group and then apply.</p>
  
  <p>Output I am getting: </p>
  
  <pre><code>f1_index
  141.0     [&lt;function mapfun at 0x7fee93550f28&gt;]
  211.0     [&lt;function mapfun at 0x7fee93550f28&gt;]
  263.0     [&lt;function mapfun at 0x7fee93550f28&gt;]
  dtype: object
  </code></pre>
  
  <p><strong>Expected output:</strong></p>
  
  <p><strong>same as final output</strong> but the output (functional part) grouped by the common index values</p>
  
  <p><strong>Now, I want to take this function and applymap in this column by grouping the data/frame using the values in one of the column or index.</strong></p>
  
  <pre><code>data_groupby = (df+df.shift(1)).dropna(how='all').\
  applymap(fnc) using groupby
  </code></pre>
  
  <p>I tried resetting the index and then groupby using the index name. But, the def fnc() is specific to the data from columns A, B, C, D.
  Also, I am not finding any examples and tutorial that uses applymap along with groupby in pandas df.</p>