The pandas package in Python is renowned for the efficient data manipulation of large datasets. Therefore, we are going to discuss the itertuples()
function of the DataFrame class.
The DataFrame.itertuples()
method returns an iterator object of DataFrame, returning each row as a Python tuple.
DataFrame.itertuples(index=True, name='Pandas')
It takes the following argument values:
index
: Boolean type, default is True
True
, it returns the index as the first element of the tuple.name
: The string or None
type, the default value is 'pandas'
.'pandas'
, it returns pandas named tuples.It returns an iterator of DataFrame, to iterate over the named tuples for each row.
In the code snippet below, we are going to create an instance of DataFrame class and invoke itertuples()
to extract each row of DataFrame as a tuple.
import pandas as pd# a data dictionarydata = {"Name": ["Sally", "Forgins", "Jhony", "Nourine"],"Designation": ["HR", "Software Engineer", "System Enginner", "Finance"],"Salary" :["$10,500","$12,000","$14,000","13,200"]}# creating DataFrame with default feature namesdf = pd.DataFrame(data)# iterate through each row# to get each row as a tuplefor row in df.itertuples():print(row)
pd.DataFrame()
to create a data frame on the dictionary created above.itertuples()
.