﻿ Pandas Practice Set-1: Iterate through diamonds DataFrame - w3resource

# Pandas Practice Set-1: Iterate through diamonds DataFrame

## Pandas Practice Set-1: Exercise-22 with Solution

Write a Pandas program to iterate through diamonds DataFrame.

Sample Solution:

Python Code:

``````import pandas as pd
print("Original Dataframe:")
print("\nIterate through diamonds DataFrame:")
for index, row in diamonds.iterrows():
print(index, row.carat, row.cut, row.color, row.price)
``````

Sample Output:

```53442 0.92 Very Good D 2674
53443 0.75 Good E 2674
53445 0.72 Ideal F 2674
53446 0.72 Ideal F 2674
53447 0.72 Ideal E 2674
53448 0.73 Very Good F 2675
53449 0.52 Ideal E 2675
53450 0.7 Very Good G 2676
53451 0.7 Very Good G 2676
53452 0.91 Good I 2676
53453 0.56 Ideal F 2676
53454 0.7 Ideal F 2676
53455 0.7 Very Good D 2677
......

53921 0.7 Very Good E 2755
53922 0.7 Very Good D 2755
53923 0.73 Ideal I 2756
53924 0.73 Ideal I 2756
53925 0.79 Ideal I 2756
53926 0.71 Ideal E 2756
53927 0.79 Good F 2756
53929 0.71 Ideal G 2756
53932 0.7 Very Good E 2757
53933 0.7 Very Good E 2757
53935 0.72 Ideal D 2757
53936 0.72 Good D 2757
53937 0.7 Very Good D 2757
53939 0.75 Ideal D 2757
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

﻿

## Python: Tips of the Day

Python: Use Enumerate() In for Loops

```>>> students = ('John', 'Mary',  'Mike')
>>> for i, student in enumerate(students):
...     print(f'Iteration:  {i}, Student: {student}')
...
Iteration: 0, Student: John
Iteration: 1, Student: Mary
Iteration: 2, Student: Mike
>>> for i, student in enumerate(students,  35001):
...      print(f'Student Name: {student}, Student ID #: {i}')
...
Student Name: John, Student ID #: 35001
Student Name: Mary, Student ID #: 35002
Student Name: Mike, Student ID #: 35003
```