title: Unable to apply function to selected columns in pandas data frame
body: <p>I have a dataframe like below:</p>
  
  <pre><code>df = pd.DataFrame(data=[[[1,0],[1,1,1,1,1,1,1,1,1,1],[1,0,0,1,1,0,0,1,1],[2,3],[1,1,1,2,2,2,3,3,3,1,1,1]]], columns=['A', 'B', 'C', 'D', 'E'])
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/w9q97.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w9q97.png" alt="enter image description here"></a></p>
  
  <p>I want to apply the below functions to selected columns in the data frame:</p>
  
  <pre><code>def item_exclude_all(list1):
      from itertools import groupby
      out = [i[0] for i in groupby(list1)]
      return out
  
  def fl(x):
      new = []
      for i in x:
          new.append([i[0], i[-1]])
      return new
  </code></pre>
  
  <p>I did the following operation by applying "fl" function, which worked without error:</p>
  
  <pre><code>df.apply(lambda x : fl(x) if x.name in ['C', 'E'] else x)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OTmjZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OTmjZ.png" alt="enter image description here"></a></p>
  
  <p>But when i'm trying to apply the other function "item_exclude_all", function is not getting applied to the selected columns. </p>
  
  <pre><code>df.apply(lambda x : item_exclude_all(x) if x.name in ['C', 'E'] else x)
  </code></pre>
  
  <p>I'm trying to understand what could be the issue here in the above code.</p>
  
  <p><a href="https://i.stack.imgur.com/6L6AE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6L6AE.png" alt="enter image description here"></a></p>