Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the starting index of iterrows()?

We can use the following to iterate rows of a data frame.

for index, row in df.iterrows(): 

What if I want to begin from a different row index? (not from first row)?

like image 766
SaikiHanee Avatar asked Jul 26 '16 17:07

SaikiHanee


People also ask

What is index in Iterrows?

iterrows() is used to iterate over a pandas Data frame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in form of series. Syntax: DataFrame.iterrows() Yields: index- The index of the row.

What is the basic function of Iterrows () function?

The iterrows() method generates an iterator object of the DataFrame, allowing us to iterate each row in the DataFrame. Each iteration produces an index object and a row object (a Pandas Series object).

What is the use of Iterrows () and Iteritems () Explain with proper examples?

This function returns each index value along with a series that contain the data in each row. iterrows() - used for iterating over the rows as (index, series) pairs. iteritems() - used for iterating over the (key, value) pairs. itertuples() - used for iterating over the rows as namedtuples.

Is Itertuples faster than Iterrows?

Itertuples(): Itertuples() iterates through the data frame by converting each row of data as a list of tuples. itertuples() takes 16 seconds to iterate through a data frame with 10 million records that are around 50x times faster than iterrows().


2 Answers

i know this has an answer, but why not just do:

for i, r in df.iloc[1:].iterrows(): 
like image 183
acushner Avatar answered Oct 01 '22 17:10

acushner


Try using itertools.islice

from itertools import islice  for index, row in islice(df.iterrows(), 1, None): 
like image 35
imreal Avatar answered Oct 01 '22 17:10

imreal