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)
Credit To: stackoverflow.com
Related Query
- Pandas series as namedtuple
- Pretty-print an entire Pandas Series / DataFrame
- Converting a Pandas GroupBy output from Series to DataFrame
- Combining two Series into a DataFrame in pandas
- How do I convert a pandas Series or index to a Numpy array?
- What is the difference between a pandas Series and a single-column DataFrame?
- Efficient way to apply multiple filters to pandas DataFrame or Series
- Find element's index in pandas Series
- Appending a list or series to a pandas DataFrame as a row?
- Convert pandas data frame to series
- Convert pandas Series to DataFrame
- Pandas How to filter a Series
- Find the end of the month of a Pandas DataFrame Series
- Histogram values of a Pandas Series
- datetime to string with series in pandas
- Pandas - check if ALL values are NaN in Series
- Getting a list of indices where pandas boolean series is True
- How to iterate over Pandas Series generated from groupby().size()
- Plotting CDF of a pandas series in python
- Get particular row as series from pandas dataframe
- Creating an element-wise minimum Series from two other Series in Python Pandas
- Create a set from a series in pandas
- pandas Series to Dataframe using Series indexes as columns
- Convert Pandas Series to DateTime in a DataFrame
- Convert Python list to pandas Series
- How to check if a variable is either a python list, numpy array or pandas series
- Convert pandas series into numpy array
- assigning column names to a pandas series
- Summing over a multiindex level in a pandas series
- Convert Pandas series containing string to boolean
More Query from same tag
- Correlation analysis with multiple data in a single cell
- How to separate text data in pandas data frame in a function
- Renaming new split columns with prefix
- What is the difference between pandas dtype vs dtypes
- How reset sequence when continuation breaks
- TypeError: corr() missing 1 required positional argument: 'other'
- Avoid dropping nulls to run function
- Column in DataFrame in Pandas with value 0
- How to create a column that starts with and end with string value in another column?
- How can I unroll a multi-level pandas dataframe
- How to merge two dataframes based on file name?
- Pandas - adding new string column results in NaN
- turning pandas to pyspark expression
- Sum FIelds From an Object to a Given Number to Solve For Maximum in Python
- Apply function to each n rows pandas