title: Loop, iloc and loc in Dataframe?
body: <p>Hey guys I'm new to python, right now I'm starting to work with some libraries such as Pandas and Numpy. Recently, my teacher gives me this excercise, and I don't know which method I should use. Details are shown below:</p>
  
  <pre><code>df1 = pd.DataFrame({'col1': [0, 1, 1, 0, 3],
                 'col2': [23, 4, 1, 1, 3], 
                 'col3': [0, 5, 2, 1, 1],
                 'col4': [1, 2, 6, 4, 0],
                 'col5': [4, 15, 0, 2, 5],
                 'loc': [1, 4, 2, 3, 2]}) 
  </code></pre>
  
  <p>1) col1 - col5: random number</p>
  
  <p>2) loc: the location of the value. </p>
  
  <p>3) Calculate 'val' which returns the value of each column, locations are given in 'loc'.</p>
  
  <p>Example: In line 0, loc = 1, val = 23. In line 1 loc = 4, val = 15, etc.</p>
  
  <p>The result should be like this:</p>
  
  <pre><code>df = pd.DataFrame({'col1': [0, 1, 1, 0, 3],
                 'col2': [23, 4, 1, 1, 3], 
                 'col3': [0, 5, 2, 1, 1],
                 'col4': [1, 2, 6, 4, 0],
                 'col5': [4, 15, 0, 2, 5],
                 'loc': [1, 4, 2, 3, 2],
                  'val': [23, 15, 2, 4, 1]})
  </code></pre>
  
  <p>I have tried somthing like iloc and loc to calculate 'val'. However when the dataframe becomes larger I could not use this method anymore. Are there any faster way to calculate 'val'? Do I need to use loop to calculate 'val'?</p>
  
  <pre><code>df1 = df['loc']
  df.iloc[0,df1[0]]
  df.iloc[1,df1[1]]
  df.iloc[2,df1[2]]
  </code></pre>
  
  <p>PS: Sorry for my bad English, but I really don't know how to explain this excercise in English, I just try my best :(</p>