title: How to speed up function in pandas
body: <p>Looking for some opportunities to speed up this process. I am using it to build a hierarchy up to 20 levels. So I call this function on the dataframe and tell it which columns to review. My dataframe varies in length a bit but it is generally 150 columns wide. Calling this function 15 times (to "climb the hierearchy") for 30k records it takes me about 45 minutes (its very slow in the first few columns it reviews). I anticipate using this function for a dataframe about 400k long and I want to speed up this process if possible. </p>
  
  <p>In a nutshell, My Dataframe should have already pulled in everyone needed to build a hierarchy at this point and then I return a unique list of Supervisor ID's. This function then loc's to those id's in the specified column (SupervisorColumnToReview) and determines the id that they report to and puts it in another specified column (ColumnToCreate). Then I just call it again on the newly created column. </p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <pre><code>def LookUpSupervisor(SampleDataFrame,SupervisorColumnToReview,ColumnToCreate):
      ColumnToReview = SupervisorColumnToReview
  
      print(ColumnToReview)
      SupervisorList=SampleDataFrame[ColumnToReview].unique().tolist()
      for Supervisor in SupervisorList:
          try:
              SampleDataFrame.loc[(SampleDataFrame[ColumnToReview] == Supervisor),ColumnToCreate] = SampleDataFrame.loc[SampleDataFrame['Employee ID'] == Supervisor,'Supervisor ID'].iloc[0]
  
          except:
              SampleDataFrame.loc[(SampleDataFrame[ColumnToReview] == Supervisor),ColumnToCreate] = "Supervisor ID Not Found"
      SampleDataFrame[ColumnToCreate] = SampleDataFrame[ColumnToCreate].astype(str)
  </code></pre>