title: How can I use a custom function that returns multiple values in a Python for loop?
body: <p>Overview: I am creating a recommendation system that compares a course already taken by a student to a catalog of available courses the student has not yet taken. The recommendation system will return 3 courses of recommended courses.</p>
  <p>Issue: Using a custom recommendation function that returns 3 values in a for loop that iterates through a transcript to compare already taken classes. The loop essentially finds/recommends the 3 classes that the student should take next. The issue is all the classes appear in one column cell and I have not found an easy way to break the column into separate rows.</p>
  <p>Deeper dive:</p>
  <p>I have a function (c_recommend) that returns 3 recommendations in the form of a series:</p>
  <p>output: Series</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>INDEX</th>
  <th>Program Title</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>123</td>
  <td>program 1</td>
  </tr>
  <tr>
  <td>456</td>
  <td>program 2</td>
  </tr>
  <tr>
  <td>789</td>
  <td>program 3</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I then use this function(c_recommend) inside a for loop to iterate over the rows of a transcript to find the course title to compare to the catalog of classes.</p>
  <pre><code>## created empty list
  results = list()
  
  ## run through the transcript 
  for i in transcript.index:
  ## append to the list the name of the student, the course already taken, the recommended courses (3 will appear)
  results.append([transcript['student'].loc[i],transcript['Course'].loc[i],c_recommend(transc['Course'].loc[i])])
  </code></pre>
  <p>output: List</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Student</th>
  <th>Taken Class</th>
  <th>Recommended Classes</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>111</td>
  <td>program 1</td>
  <td>program 2, program 3, program 4</td>
  </tr>
  <tr>
  <td>222</td>
  <td>program 2</td>
  <td>program 5, program 1, program 3</td>
  </tr>
  <tr>
  <td>333</td>
  <td>program 3</td>
  <td>program 2, program 1, program 4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The recommended classes are all bunched into one row due to the fact that the c_recommend function runs and returns three values. I need a way to separate those 3 values out into their own columns like so:</p>
  <p>desired output:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Student</th>
  <th>Taken Class</th>
  <th>Recommended Classes</th>
  <th>Reco Class 2</th>
  <th>Reco Class 3</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>111</td>
  <td>program 1</td>
  <td>program 2</td>
  <td>program 3</td>
  <td>program 4</td>
  </tr>
  <tr>
  <td>222</td>
  <td>program 2</td>
  <td>program 5</td>
  <td>program 1</td>
  <td>program 3</td>
  </tr>
  <tr>
  <td>333</td>
  <td>program 3</td>
  <td>program 2</td>
  <td>program 1</td>
  <td>program 4</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have tried converting the list to a pandas dataframe and separating, using regex to split the commas, using nested loops. Alas, I have failed and the columns does not separate :( Ideally after this issue is fixed, I would like to convert this to a pandas DF. Maybe there is an easier way to handle this with pandas?</p>
  <p>I would appreciate all and any insight even if that means rewriting my function.</p>
  <p>TIA!</p>