How to access a field of a namedtuple using a variable for the field name?
Asked Answered
S

5

115

I can access elements of a named tuple by name as follows(*):

from collections import namedtuple
Car = namedtuple('Car', 'color mileage')
my_car = Car('red', 100)
print my_car.color

But how can I use a variable to specify the name of the field I want to access? E.g.

field = 'color'
my_car[field] # doesn't work
my_car.field # doesn't work

My actual use case is that I'm iterating through a pandas dataframe with for row in data.itertuples(). 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.

(*) example taken from here. I am using Python 2.7.

Species answered 19/6, 2017 at 15:52 Comment(2)
getattr(my_car, field) or my_car._asdict()[field].Brittnee
Also try mycar[field] but then you might iterate using for row in data.Hermes
E
144

You can use getattr

getattr(my_car, field)
Eboat answered 19/6, 2017 at 15:55 Comment(0)
W
11

The 'getattr' answer works, but there is another option which is slightly faster.

idx = {name: i for i, name in enumerate(list(df), start=1)}
for row in df.itertuples(name=None):
   example_value = row[idx['product_price']]

Explanation

Make a dictionary mapping the column names to the row position. Call 'itertuples' with "name=None". Then access the desired values in each tuple using the indexes obtained using the column name from the dictionary.

  1. Make a dictionary to find the indexes.

idx = {name: i for i, name in enumerate(list(df), start=1)}

  1. Use the dictionary to access the desired values by name in the row tuples
for row in df.itertuples(name=None):
   example_value = row[idx['product_price']]

Note: Use start=0 in enumerate if you call itertuples with index=False

Here is a working example showing both methods and the timing of both methods.

import numpy as np
import pandas as pd
import timeit

data_length = 3 * 10**5
fake_data = {
    "id_code": list(range(data_length)),
    "letter_code": np.random.choice(list('abcdefgz'), size=data_length),
    "pine_cones": np.random.randint(low=1, high=100, size=data_length),
    "area": np.random.randint(low=1, high=100, size=data_length),
    "temperature": np.random.randint(low=1, high=100, size=data_length),
    "elevation": np.random.randint(low=1, high=100, size=data_length),
}
df = pd.DataFrame(fake_data)


def iter_with_idx():
    result_data = []
    
    idx = {name: i for i, name in enumerate(list(df), start=1)}
    
    for row in df.itertuples(name=None):
        
        row_calc = row[idx['pine_cones']] / row[idx['area']]
        result_data.append(row_calc)
        
    return result_data

      
def iter_with_getaatr():
    
    result_data = []
    for row in df.itertuples():
        row_calc = getattr(row, 'pine_cones') / getattr(row, 'area')
        result_data.append(row_calc)
        
    return result_data
    

dict_idx_method = timeit.timeit(iter_with_idx, number=100)
get_attr_method = timeit.timeit(iter_with_getaatr, number=100)

print(f'Dictionary index Method {dict_idx_method:0.4f} seconds')
print(f'Get attribute method {get_attr_method:0.4f} seconds')

Result:

Dictionary index Method 49.1814 seconds
Get attribute method 80.1912 seconds

I assume the difference is due to lower overhead in creating a tuple vs a named tuple and also lower overhead in accessing it by the index rather than getattr but both of those are just guesses. If anyone knows better please comment.

I have not explored how the number of columns vs number of rows effects the timing results.

Written answered 24/7, 2020 at 17:44 Comment(0)
T
6

since python version 3.6 one could inherit from typing.NamedTuple

import typing as tp


class HistoryItem(tp.NamedTuple):
    inp: str
    tsb: float
    rtn: int
    frequency: int = None

    def __getitem__(self, item):
        if isinstance(item, int):
            item = self._fields[item]
        return getattr(self, item)

    def get(self, item, default=None):
        try:
            return self[item]
        except (KeyError, AttributeError, IndexError):
            return default


item = HistoryItem("inp", 10, 10, 10)

print(item[0])  # 'inp'
print(item["inp"])  # 'inp'
Throve answered 15/12, 2020 at 7:56 Comment(4)
This needs to be recognised as the modern answer.Smutchy
Python 3.6 is not required for this, you can use the collections.namedtuple class factory and achieve the same thing.Eboat
Also, tp.NamedTuple.__getitem__ will give you an attribute error. tp.NamedTuple isn't a real class... it's kind of ugly, actually, it is merely used as a vehicle for metaclass magic that eventually calls collections.namedtuple to return a class derived from tuple.Eboat
@Eboat thanks for pointing out. I have fixed it.Throve
A
4

Another way of accessing them can be:

field_idx = my_car._fields.index(field)
my_car[field_idx]

Extract index of the field and then use it to index the namedtuple.

Aristotelian answered 6/8, 2020 at 20:12 Comment(0)
C
1

Use the following code

for i,x in enumerate(my_car._fields):
    print(x, my_car[i])
Cursed answered 2/9, 2021 at 2:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.