DataFrame.itertuples
- DataFrame.itertuples(index: bool = True, name: str | None = 'opensearch_py_ml', sort_index: str | None = '_doc') Iterable[Tuple[Any, ...]] [source]
Iterate over opensearch_py_ml.DataFrame rows as namedtuples.
Parameters
- index: bool, default True
If True, return the index as the first element of the tuple.
- name: str or None, default “opensearch-py-ml”
The name of the returned namedtuples or None to return regular tuples.
- sort_index: str, default ‘_doc’
What field to sort the OpenSearch data by.
Returns
- iterator
An object to iterate over namedtuples for each row in the DataFrame with the first field possibly being the index and following fields being the column values.
See Also
opensearch_py_ml.DataFrame.iterrows: Iterate over opensearch_py_ml.DataFrame rows as (index, pandas.Series) pairs.
Examples
>>> from tests import OPENSEARCH_TEST_CLIENT
>>> df = oml.DataFrame(OPENSEARCH_TEST_CLIENT, 'flights', columns=['AvgTicketPrice', 'Cancelled']).head() >>> df AvgTicketPrice Cancelled 0 841.265642 False 1 882.982662 False 2 190.636904 False 3 181.694216 True 4 730.041778 False [5 rows x 2 columns]
>>> for row in df.itertuples(): ... print(row) opensearch_py_ml(Index='0', AvgTicketPrice=841.2656419677076, Cancelled=False) opensearch_py_ml(Index='1', AvgTicketPrice=882.9826615595518, Cancelled=False) opensearch_py_ml(Index='2', AvgTicketPrice=190.6369038508356, Cancelled=False) opensearch_py_ml(Index='3', AvgTicketPrice=181.69421554118, Cancelled=True) opensearch_py_ml(Index='4', AvgTicketPrice=730.041778346198, Cancelled=False)
By setting the index parameter to False we can remove the index as the first element of the tuple:
>>> for row in df.itertuples(index=False): ... print(row) opensearch_py_ml(AvgTicketPrice=841.2656419677076, Cancelled=False) opensearch_py_ml(AvgTicketPrice=882.9826615595518, Cancelled=False) opensearch_py_ml(AvgTicketPrice=190.6369038508356, Cancelled=False) opensearch_py_ml(AvgTicketPrice=181.69421554118, Cancelled=True) opensearch_py_ml(AvgTicketPrice=730.041778346198, Cancelled=False)
With the name parameter set we set a custom name for the yielded namedtuples:
>>> for row in df.itertuples(name='Flight'): ... print(row) Flight(Index='0', AvgTicketPrice=841.2656419677076, Cancelled=False) Flight(Index='1', AvgTicketPrice=882.9826615595518, Cancelled=False) Flight(Index='2', AvgTicketPrice=190.6369038508356, Cancelled=False) Flight(Index='3', AvgTicketPrice=181.69421554118, Cancelled=True) Flight(Index='4', AvgTicketPrice=730.041778346198, Cancelled=False)