The items() method

The items() method provides a means of iteratively accessing each row in a series or DataFrame. It performs a lazy evaluation to store each value in a row, along with the index in the form of a tuple. The results of this lazy evaluation can be obtained through an iterative process such as a for loop. Let's apply the items method on the Wins column of the DataFrame:

for item in sample_df["Wins"].items():
print(item)

The following is the output:

Looping with the items method

The iteritems() method behaves in a similar way to the items() method:

for item in sample_df["Wins"].iteritems():
print(item)

Looping with the iteritems method

The items and iteritems methods return a zip type object. We need an iterative process to unzip the object. Applying the items or iteritems methods on a DataFrame give different results. In this case, each column is stacked within a tuple, along with the column name:

for col in sample_df.items():
print(col)

The following is the output:

Items method used on a DataFrame
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset