I am accessing pandas data frame rows and as a result I get pandas series. My parsing routines accept namedtuples. Is it possible to convert pandas series to named tuple?

score:1

Another way to go about it, if what you have in hand is already a Pandas Series and if you're using it as input to a function, is to unpack the Series as-is.

>>> df = pd.DataFrame({'name': ['John', 'Sally'], 'date': ['2020-01-01', '2020-02-01'], 'value': ['A', 'B']})
>>> df
    name        date value
0   John  2020-01-01     A
1  Sally  2020-02-01     B
>>> row = df.iloc[0]
>>> type(row)
<class 'pandas.core.series.Series'>
>>> print({**row})  # unpacks as a dictionary
{'name': 'John', 'date': '2020-01-01', 'value': 'A'}
>>> myfunc(**row)   # ergo, unpacks as keyword args

This is because a Pandas Series is already a namedtuple-like object (and it's exactly what df.itertuples returns).

Anyway, for the problem I was trying to solve I was taking a specific row of the dataframe rather than iterating over the whole thing, and so I didn't need to go the route of converting to a named tuple.

score:4

You can probably just use df.itertuples for whatever you are doing:

In [5]: df
Out[5]:
     c0    c1    c2    c3    c4    c5    c6    c7    c8    c9
0   8.0   2.0   1.0   4.0   4.0   3.0   1.0  19.0   5.0   9.0
1   7.0   7.0   0.0   4.0  14.0   7.0   9.0   0.0   0.0   9.0
2  19.0  10.0   6.0  13.0  12.0  11.0   8.0   4.0  11.0  13.0
3  14.0   0.0  16.0  19.0   3.0   8.0   8.0   9.0  17.0  13.0
4  18.0  16.0  10.0   8.0  15.0   9.0  18.0   9.0   5.0  10.0
5  15.0   7.0  16.0   3.0  18.0  14.0   3.0   6.0   0.0   9.0
6  14.0  14.0  18.0   4.0   4.0   0.0   8.0  15.0   8.0  12.0
7  19.0  16.0  15.0  16.0   1.0  12.0  14.0   1.0  10.0  15.0
8   8.0  17.0  10.0  18.0   7.0  13.0  13.0  12.0   6.0  11.0
9  15.0  13.0  13.0  17.0   2.0   0.0   6.0  10.0   5.0   5.0

In [6]: rows = df.itertuples(name='Row')

In [7]: r0 = next(rows)

In [8]: r0
Out[8]: Row(Index=0, c0=8.0, c1=2.0, c2=1.0, c3=4.0, c4=4.0, c5=3.0, c6=1.0, c7=19.0, c8=5.0, c9=9.0)

In [9]: r0.c0
Out[9]: 8.0

Otherwise, you'll have to do it yourself, something like:

In [10]: from collections import namedtuple

In [11]: df.columns
Out[11]: Index(['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9'], dtype='object')

In [12]: Row = namedtuple('Row', df.columns)

In [13]: df.iloc[0]
Out[13]:
c0     8.0
c1     2.0
c2     1.0
c3     4.0
c4     4.0
c5     3.0
c6     1.0
c7    19.0
c8     5.0
c9     9.0
Name: 0, dtype: float64

In [14]: Row(*df.iloc[0])
Out[14]: Row(c0=8.0, c1=2.0, c2=1.0, c3=4.0, c4=4.0, c5=3.0, c6=1.0, c7=19.0, c8=5.0, c9=9.0)

Note, this version doesn't have an index field...

score:6

General purpose function to convert any series into a namedtuple

def namedtuple_me(s, name='S'):
    return namedtuple(name, s.index)(*s)

namedtuple_me(pd.Series([1, 2, 3], list('abc')))
S(a=1, b=2, c=3)

For improved implementation courtesy of @juanpa.arrivillaga

import functools
from collections import namedtuple

@functools.lru_cache(maxsize=None)  # add memoization to increase speed
def _get_class(fieldnames, name):
    """Create a new namedtuple class."""
    return namedtuple(name, fieldnames)

def namedtuple_me(series, name='S'):
    """Convert the series to a namedtuple."""
    klass = _get_class(tuple(series.index), name)
    return klass._make(series)

Related Query