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?
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework