Pandas DataFrame itertuples() Method
In this tutorial, we will learn the Python pandas DataFrame.itertuples()
method. The DataFrame.itertuples()
method iterates the rows of the DataFrame as namedtuples
. When this method applied on the DataFrame, it returns a map object. We can use the returned object to iterate over namedtuples for each row in the DataFrame with the first field possibly being the index and the following fields being the column values.
The below shows the syntax of the DataFrame.itertuples()
method.
Syntax
DataFrame.itertuples(index=True, name='Pandas')
Parameters
index: It is bool. The default is True.
If this parameter is True, this function returns the index as the first element of the tuple.
name: It is str or None. The default is “Pandas”. It represents the name of the returned namedtuples. If it is None, it returns regular tuples.
Example: Iterate over the rows of the DataFrame
In the below example, we create the DataFrame and using the DataFrame.itertuples()
method. We try to iterate the rows of the DataFrame. The DataFrame.itertuples()
method returns the map
object.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over DataFrame rows---------")
print(df.itertuples())
-----------The DataFrame is---w----
Name Age Education
index_1 Navya 25 M.Tech
index_2 Vindya 24 Ph.d
---------Iterate over DataFrame rows---------
<map object at 0x000001C9899720A0>
Example: The DataFrame.itertuples()
Method
In the previous example, we can see the DataFrame.itertuples()
method returns the map object to iterate over the rows of the DataFrame. We can use the for
loop to display the rows of the DataFrame as a namedtuples
.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over DataFrame rows---------")
for rows in df.itertuples():
print(rows)
As the 'index
' and 'name
' parameter of the DataFrame.itertuples()
method is 'True
' and 'Pandas
' respectively, it returns the first element tuple as index and name of the returned tuples as 'Pandas
'.
-----------The DataFrame is-------
Name Age Education
index_1 Navya 25 M.Tech
index_2 Vindya 24 Ph.d
---------Iterate over DataFrame rows---------
Pandas(Index='index_1', Name='Navya', Age=25, Education='M.Tech')
Pandas(Index='index_2', Name='Vindya', Age=24, Education='Ph.d')
Example: Set index=False
in the DataFrame.itertuples()
Method
If the index parameter of the DataFrame.itertuples()
is 'False
', we can remove the index
as the first element of the tuple. See the below example.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over DataFrame rows---------")
for rows in df.itertuples(index=False):
print(rows)
In the below output we can see that, the DataFrame.itertuples()
function does not display the index as a tuple.
----------The DataFrame is-------
Name Age Education
index_1 Navya 25 M.Tech
index_2 Vindya 24 Ph.d
---------Iterate over DataFrame rows---------
Pandas(Name='Navya', Age=25, Education='M.Tech')
Pandas(Name='Vindya', Age=24, Education='Ph.d')
Example: The DataFrame.itertuples()
Method
We can specify the name of the returned namedtuples using the name
method of the DataFrame.itertuples()
function. The default name will be removed and the specified name will be displayed. See the below example.
#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])
print("-----------The DataFrame is-------")
print(df)
print("---------Iterate over DataFrame rows---------")
for rows in df.itertuples( name='Rows'):
print(rows)
As we can see the name of the namedtuples is displayed as 'Rows
'.
-----------The DataFrame is-------
Name Age Education
index_1 Navya 25 M.Tech
index_2 Vindya 24 Ph.d
---------Iterate over DataFrame rows---------
Rows(Index='index_1', Name='Navya', Age=25, Education='M.Tech')
Rows(Index='index_2', Name='Vindya', Age=24, Education='Ph.d')
Conclusion
In this tutorial, we learned the Python pandas DataFrame.itertuples()
method . We learned the syntax and by applying this method on the DataFrame we solved examples and understood the DataFrame.itertuples()
method.