title: Filter pandas DataFrame by string length within group
body: <p>Let's say I have the following data</p>
  
  <pre><code>import pandas as pd
  df = pd.DataFrame(data=[[1, 'a'], [1, 'aaa'], [1, 'aa'], 
                          [2, 'bb'], [2, 'bbb'], 
                          [3, 'cc']], 
                    columns=['key', 'text'])
  
     key text
  0    1    a
  1    1  aaa
  2    1   aa
  3    2   bb
  4    2  bbb
  5    3   cc
  </code></pre>
  
  <p>What I would like to do is group by the <code>key</code> variable and sort the data within each group by the length of <code>text</code> and end up with a single <code>Series</code> of index values to use to reindex the dataframe. I thought I could just do something like this:</p>
  
  <pre><code>df.groupby('key').text.str.len().sort_values(ascending=False).index
  </code></pre>
  
  <p>But it said I need to use <code>apply</code>, so I tried this:</p>
  
  <pre><code>df.groupby('key').apply(lambda x: x.text.str.len().sort_values(ascending=False).index, axis=1)
  </code></pre>
  
  <p>But that told me that <code>lambda</code> got an unexpected keyword: <code>axis</code>. </p>
  
  <p>I'm relatively new to pandas, so I'm not sure how to go about this. Also, my goal is to simply deduplicate the data such that for each <code>key</code>, I keep the value with the longest value of <code>text</code>. The expected output is:</p>
  
  <pre><code>   key text
  1    1  aaa
  4    2  bbb
  5    3   cc
  </code></pre>
  
  <p>If there's an easier way to do this than what I'm attempting, I'm open to that as well.</p>