title: Count matching combinations in a pandas dataframe
body: <p>I need to find a more efficient solution for the following problem:</p>
  
  <p>Given is a dataframe with 4 variables in each row. I need to find the list of 8 elements that includes all the variables per row in a maximum amount of rows.</p>
  
  <p>A working, but very slow, solution is to create a second dataframe containing all possible combinations (basically a permutation without repetation). Then loop through every combination and compare it wit the inital dataframe. The amount of solutions is counted and added to the second dataframe.</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  from itertools import combinations
  
  
  df = pd.DataFrame(np.random.randint(0,20,size=(100, 4)), columns=list('ABCD'))
  df = 'x' + df.astype(str)
  listofvalues = df['A'].tolist()
  listofvalues.extend(df['B'].tolist())
  listofvalues.extend(df['C'].tolist())
  listofvalues.extend(df['D'].tolist())
  listofvalues = list(dict.fromkeys(listofvalues))
  possiblecombinations = list(combinations(listofvalues, 6))
  dfcombi = pd.DataFrame(possiblecombinations, columns = ['M','N','O','P','Q','R'])
  dfcombi['List'] = dfcombi.M.map(str) + ',' + dfcombi.N.map(str) + ',' + dfcombi.O.map(str) + ',' + dfcombi.P.map(str) + ',' + dfcombi.Q.map(str) + ',' + dfcombi.R.map(str)
  dfcombi['Count'] = ''
  for x, row in dfcombi.iterrows():
          comparelist =  row['List'].split(',')
          pointercounter = df.index[(df['A'].isin(comparelist) == True) &amp; (df['B'].isin(comparelist) == True) &amp; (df['C'].isin(comparelist) == True) &amp; (df['D'].isin(comparelist) == True)].tolist()
          row['Count'] = len(pointercounter)
  </code></pre>
  
  <p>I assume there must be a way to avoid the for - loop and replace it with some pointer, i just can not figure out how.</p>
  
  <p>Thanks!</p>