0

Pandas: Iterating DataFrame

 11 months ago
source link: https://dzone.com/articles/pandas-iterating-dataframe
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Pandas: Iterating DataFrame

This article focuses on various data analysis options available in the Pandas library for iterating the DataFrame with an example taken from Kaggle Dataset.

Dec. 06, 21 · Big Data Zone · Review

Join the DZone community and get the full member experience.

Join For Free

This article focuses on various options available in Pandas for iterating the DataFrame. To begin with, Pandas is a library used mainly for Data Analysis purposes and a DataFrame is a two-dimensional data structure like a table to hold rows and columns. 

The example used in this article is taken from Kaggle Dataset for Trending YouTube video statistics. 

Kaggle Dataset for Trending YouTube video statistics

There are multiple options available for us to iterate through the DataFrame let’s dive in. 

Iterrows: Iterrows is a function that is used to Iterate over Panda DataFrame rows as index and Series pair. Pandas Series is a one-dimensional data structure (array-like) that can hold any data types of Integers, Floats, Strings, etc.

Iterrows Function

Output: 

Iterrows Function Output

Itertuples:  Itertuples is a function that is used to Iterate over Panda DataFrame rows as namedtuples, namedtuple is like a container by using it we can access the elements using a member of a tuple instead of index.

Itertuples Function

Output:

Itertuples Function Output

Iteritems:  Iteritems is a function that is used to Iterate over Panda DataFrame rows as key and value pairs, with label = key and column = Series object.

Iteritems Function Output

loc:  The loc property is used to access rows and columns by labels, meaning we must specify the index and the title of the column to access the data. For example, in our YouTube Video dataset, the first title is "WE WANT TO TALK ABOUT OUR MARRIAGE":

 loc Property

loc Property

 Similarly, for iterating through the DataFrame we will use the same principal:

loc Property Principal

Output: 

loc Function Output

Please note, in loc and iloc multiple columns can be combined. 

iloc:  The iloc property is used to access rows and columns by index instead of labels

iloc Property

In the example 2 = title column and 7 = view column. 

Output: 

iloc Property Output

I hope you find it useful, Thank You for reading. 


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK