title: Only keep pandas columns where value_count of all values greater than some threshold;
body: <p>I need to drop columns where:</p>
  
  <ul>
  <li>The value_counts of any unique value is below some threshold </li>
  </ul>
  
  <p><code>(s.value_counts() &gt; THRESHOLD).all()</code></p>
  
  <ul>
  <li>OR number of unique values is greater than some other threshold </li>
  </ul>
  
  <p><code>nunique() &gt; OTHER_THRESH</code></p>
  
  <p>I tried to use <a href="https://stackoverflow.com/questions/22320356/pandas-get-values-from-column-that-appear-more-than-x-times">Pandas: Get values from column that appear more than X times</a> to get the value counts across all columns, but I'm stuck on the indexing.</p>
  
  <pre><code>&gt;&gt;&gt; test
        col1  col2    a    b     c
  col1                            
  1      0.0     3  5.0  6.0   7.0
  2      0.0     4  8.0  9.0  10.0
  
  &gt;&gt;&gt; test.apply(lambda s: (s.value_counts() &gt; 1).all() if s.nunique() &lt; 3 else s.nunique() &gt; 1)
  col1     True
  col2    False
  a       False
  b       False
  c       False
  
  &gt;&gt;&gt; test[test.apply(lambda s: (s.value_counts() &gt; 1).all() if s.nunique() &lt; 3 else s.nunique() &gt; 1).index]
        col1  col2    a    b     c
  col1                            
  1      0.0     3  5.0  6.0   7.0
  2      0.0     4  8.0  9.0  10.0
  </code></pre>
  
  <p>I wanted just <code>col1</code> in the example, but got everything again. I could just iterate over column names:</p>
  
  <pre><code>&gt;&gt;&gt; asdf = test.apply(lambda s: (s.value_counts() &gt; 1).all() if s.nunique() &lt; 3 else s.nunique() &gt; 1)
  &gt;&gt;&gt; test[asdf[asdf == True].index]
        col1
  col1      
  1      0.0
  2      0.0
  </code></pre>
  
  <p>But I'm not sure that this is the "correct"/standard way to do it (standard meaning efficient and legible). Assigning <code>asdf</code> to an entire apply function and then extracting its index seems overly hacky/complicated. How can I use pandas more effectively here to ensure efficient computation?</p>