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
diamonds = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/diamonds.csv')
print("Original Dataframe:")
print(diamonds.head())
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 53444 0.71 Premium 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 53928 0.79 Premium E 2756 53929 0.71 Ideal G 2756 53930 0.71 Premium E 2756 53931 0.71 Premium F 2756 53932 0.7 Very Good E 2757 53933 0.7 Very Good E 2757 53934 0.72 Premium D 2757 53935 0.72 Ideal D 2757 53936 0.72 Good D 2757 53937 0.7 Very Good D 2757 53938 0.86 Premium H 2757 53939 0.75 Ideal D 2757
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to read only a subset of 3 rows from diamonds DataFrame.
Next: Write a Pandas program to drop all non-numeric columns from diamonds DataFrame.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/pandas/practice-set1/pandas-practice-set1-exercise-22.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics