title: Groupby Apply/Transform Custom Function With Arguments Pandas
body: <p>I'm doing some NLP work and I am trying to use groupby to do a post request inside of a lambda function and am getting a JSON object response that, unfortunately, results in <code>NaN</code>.  I need it to result in adding the fields after 'exploding' them.</p>
  <p>Custom function:</p>
  <pre><code>def posTagger(text):
      post = { &quot;text&quot;: title }
      endpoint = 'http://localhost:8001/api/postagger'
      r = requests.post(endpoint, json=post)
      r = r.json()
      time.sleep(1)
      return {&quot;title&quot;: title, &quot;result&quot;: r}
  
  
  </code></pre>
  <p><code>posTagger</code> return value:</p>
  <pre><code>[
      {
          &quot;text&quot;: &quot;Contemporary Modern Soft Area Rugs Nonslip&quot;,
          &quot;terms&quot;: [
              {
                  &quot;text&quot;: &quot;Contemporary&quot;,
                  &quot;penn&quot;: &quot;JJ&quot;,
                  &quot;tags&quot;: [
                      &quot;Adjective&quot;
                  ]
              },
              {
                  &quot;text&quot;: &quot;Modern&quot;,
                  &quot;penn&quot;: &quot;NNP&quot;,
                  &quot;tags&quot;: [
                      &quot;ProperNoun&quot;,
                      &quot;Noun&quot;,
                      &quot;Singular&quot;
                  ]
              },
              {
                  &quot;text&quot;: &quot;Soft&quot;,
                  &quot;penn&quot;: &quot;NNP&quot;,
                  &quot;tags&quot;: [
                      &quot;ProperNoun&quot;,
                      &quot;Noun&quot;,
                      &quot;Singular&quot;
                  ]
              },
              {
                  &quot;text&quot;: &quot;Area&quot;,
                  &quot;penn&quot;: &quot;NN&quot;,
                  &quot;tags&quot;: [
                      &quot;Singular&quot;,
                      &quot;Noun&quot;,
                      &quot;ProperNoun&quot;
                  ]
              },
              {
                  &quot;text&quot;: &quot;Rugs&quot;,
                  &quot;penn&quot;: &quot;NNP&quot;,
                  &quot;tags&quot;: [
                      &quot;ProperNoun&quot;,
                      &quot;Noun&quot;,
                      &quot;Plural&quot;
                  ]
              },
              {
                  &quot;text&quot;: &quot;Nonslip&quot;,
                  &quot;penn&quot;: &quot;NNP&quot;,
                  &quot;tags&quot;: [
                      &quot;ProperNoun&quot;,
                      &quot;Noun&quot;,
                      &quot;Singular&quot;
                  ]
              }
          ]
      }
  ]
  </code></pre>
  <p>DataFrame</p>
  <pre><code>title = [
      'Contemporary Modern Soft Area Rugs Nonslip Velvet Home Room Carpet Floor Mat Rug', 
      'Traditional Distressed Area Rug 8x10 Large Rugs for Living Room 5x8 Gray Ivory', 
      'Shaggy Area Rugs Fluffy Tie-Dye Floor Soft Carpet Living Room Bedroom Large Rug'
      ]
  df = pd.DataFrame(title, columns=['title'])
  df
  
  # Initial dataframe:
  
  # title
  # 0 Contemporary Modern Soft Area Rugs Nonslip...
  # 1 Traditional Distressed Area Rug 8x10 Large...
  # 2 Shaggy Area Rugs Fluffy Tie-Dye Floor Soft...
  </code></pre>
  <p>So, here's my groupby using <strong>.apply</strong>:</p>
  <pre><code>df['result'] = pd.DataFrame(df.groupby(['title']).apply(lambda x: posTagger(x)))
  df
  
  # Resulting DataFrame after **.apply**:
  
  #   title   result
  # 0 Contemporary Modern Soft Area Rugs Nonslip Vel...   NaN
  # 1 Traditional Distressed Area Rug 8x10 Large Rug...   NaN
  # 2 Shaggy Area Rugs Fluffy Tie-Dye Floor Soft Car...   NaN
  </code></pre>
  <p>So, here's my groupby using <strong>.transform</strong>:</p>
  <pre><code>df['result'] = pd.DataFrame(df.groupby(['title']).transform(lambda x: posTagger(x)))
  df
  
  # Resulting DataFrame after **.transform**:
  
  # title result
  # 0 Contemporary Modern Soft Area Rugs Nonslip Vel...   {'title': ['Contemporary Modern Soft Area Rugs...
  # 1 Traditional Distressed Area Rug 8x10 Large Rug...   {'title': ['Contemporary Modern Soft Area Rugs...
  # 2 Shaggy Area Rugs Fluffy Tie-Dye Floor Soft Car...   {'title': ['Contemporary Modern Soft Area Rugs...
  </code></pre>
  <p>Notice, <code>.transform</code>'s result sent the <strong>same value</strong> multiple times.  <em>Why?</em></p>
  <ol>
  <li>How do I get return value from the custom function (which returns an object with nested arrays) to be added in exploded form to the same dataframe as new columns?</li>
  <li>Is it better to use <code>.apply</code> or <code>.transform</code> to achieve this?</li>
  </ol>