# How To Iterate Over Rows In A Dataframe In Pandas

But if one has to loop through dataframe, there are mainly two ways to iterate rows.

- iterrows()
- itertuples()

Let us download a following CSV data from the given link.

In [1]:

```
!wget http://faculty.marshall.usc.edu/gareth-james/ISL/College.csv
```

In [2]:

```
import pandas as pd
import time
```

In [3]:

```
df = pd.read_csv('College.csv')
```

In [4]:

```
df.head(1)
```

Out[4]:

In [5]:

```
len(df)
```

Out[5]:

There are 777 rows in our dataframe.

## Loop through dataframe using iterrows()

In [6]:

```
st = time.time()
for index, row in df.iterrows():
i,r = index,row['Apps']*1
end = time.time()
```

In [7]:

```
print(end-st)
```

## Loop through dataframe using itertuples()

In [8]:

```
st = time.time()
for row in df.itertuples():
apps = row.Apps*1
end = time.time()
```

In [9]:

```
print(end-st)
```

## Loop through dataframe using apply()

In [18]:

```
st = time.time()
df['Apps'] = df.apply(lambda x: x['Apps']*1,axis=1)
end = time.time()
```

In [20]:

```
print(end-st)
```

## Related Notebooks

- How to Convert Python Pandas DataFrame into a List
- How to Export Pandas DataFrame to a CSV File
- How to Plot a Histogram in Python
- How To Drop One Or More Columns In Pandas Dataframe
- How to Create DataFrame in R Using Examples
- How To Write DataFrame To CSV In R
- Python Pandas String To Integer And Integer To String DataFrame
- 3 Ways to Rename Columns in Pandas DataFrame
- How To Replace na Values with Zeros In R Dataframe