title: How to filter a dataframe multiple times in a loop with Pandas (multiple conditions and one-to-many dataframe results)
body: <p>I'll show a snippet of the code later, the problem is as following:</p>
  <p>I have a dataframe, and a list with some columns of that dataframe. I need to take all distinct values of those columns, store them, and make a unique dataframe for each combination of those distinct values in the original dataframe. Then, export those many dataframes to an excel (no problem with that). For example:</p>
  <p><a href="https://i.stack.imgur.com/ZG90l.jpg" rel="nofollow noreferrer">example-table</a></p>
  <p>That table would be converted to a dataframe, and let's suppose the list for columns is ['OS', 'Work']. In the end, I'll have a dictionary with each column as key and each distinct value as a set of values for that key, as follows:</p>
  <p>data = {'OS': {'IOS', 'Linux', 'Windows'}, 'Work': {'Developer', 'CEO', 'Administrator', 'Engineer'}}</p>
  <p>Now comes the problem (and the code block I'll show). I need to filter the dataframe according to combinations of those values, for example:</p>
  <p>Dataframe 1) IOS + Developer ---&gt; Will only have all rows that have IOS in the OS column, and Developer in the Work column</p>
  <p>Dataframe 2) IOS + CEO ---&gt; Will only have all rows that have IOS in the OS column, and CEO in the Work column</p>
  <p><strong>It is important to notice, I have no idea of what columns or dataframe will be entered, meaning it could be any number of columns, with any number of distinct values, and the algorithm should work for all cases</strong></p>
  <p>This is the code I have so far:</p>
  <pre><code># data is the dictionary with the values as shown, it will automatically get all
  # the columns and distinct values, for any number of columns and any dataframe
  
  # column_name is the name of the column that I'm about to filter, and N is the condition
  # (for example, df['OS'] == 'Linux' will only take rows that have Linux in that column
  
  for N in data:
      out = path + f'{name}({N})'
      df_aux = df[df[column_name] == N]
      with pandas.ExcelWriter(out) as writer:
          #... and it exports the dataframe to an excel .xlsx file
  
  # this works for one column (working with a string and a set instead of a dictionary),
  # but I have this (failure) for multiple columns
  
  for col in data:
      for N in data[col]:
          #... and then filter with
          df_aux = df[df[col] == N]
  
  #...and then export it to excel file in this level of indentation
  </code></pre>
  <p>I've tried different levels of indentation, using a multidimensional array instead of a dictionary, using an ordered dictionary, ... in the end, I really don't know how to make the loop work, and that's the core issue. My idea right now is to make a dataframe with the distinct values of the columns, and simply make all the different possibilities walking through the dataframe, but still, I don't know how to do the loop, because I don't know how to filter the original dataframe with an arbitraty number of conditions.</p>
  <p>Thanks for the help, I know it's a bit of a lengthy question.</p>