title: How to access a field of a namedtuple using a variable for the field name?
body: <p>I can access elements of a named tuple by name as follows(*):</p>
  
  <pre><code>from collections import namedtuple
  Car = namedtuple('Car', 'color mileage')
  my_car = Car('red', 100)
  print my_car.color
  </code></pre>
  
  <p>But how can I use a variable to specify the name of the field I want to access? E.g.</p>
  
  <pre><code>field = 'color'
  my_car[field] # doesn't work
  my_car.field # doesn't work
  </code></pre>
  
  <p>My actual use case is that I'm iterating through a pandas dataframe with <code>for row in data.itertuples()</code>. I am doing an operation on the value from a particular column, and I want to be able to specify the column to use by name as a parameter to the method containing this loop.</p>
  
  <p>(*) <a href="https://dbader.org/blog/writing-clean-python-with-namedtuples" rel="noreferrer">example taken from here</a>. I am using Python 2.7.</p>