Pandas Practice Set-1: Sort the entire diamonds DataFrame by the 'carat' Series in ascending and descending order
Pandas Practice Set-1: Exercise-14 with Solution
Write a Pandas program to sort the entire diamonds DataFrame by the 'carat' Series in ascending and descending order.
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("\nSort the entire diamonds DataFrame by the 'carat' Series in ascending order")
result = diamonds.sort_values('carat')
print(result)
print("\nSort the entire diamonds DataFrame by the 'carat' Series in descending order")
result = diamonds.sort_values('carat', ascending=False)
print(result)
Sample Output:
Original Dataframe: carat cut color clarity depth table price x y z 0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43 1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31 2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31 3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63 4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75 Sort the entire diamonds DataFrame by the 'carat’ Series in ascending order carat cut color clarity ... price x y z 31593 0.20 Premium E VS2 ... 367 3.81 3.78 2.32 31597 0.20 Ideal D VS2 ... 367 3.81 3.77 2.33 31596 0.20 Premium F VS2 ... 367 3.73 3.71 2.33 31595 0.20 Ideal E VS2 ... 367 3.86 3.84 2.30 31594 0.20 Premium E VS2 ... 367 3.84 3.80 2.28 31600 0.20 Premium D VS2 ... 367 3.73 3.68 2.31 31592 0.20 Premium E VS2 ... 367 3.81 3.78 2.24 31591 0.20 Premium E VS2 ... 367 3.79 3.77 2.26 31601 0.20 Premium D VS2 ... 367 3.77 3.72 2.31 31598 0.20 Very Good E VS2 ... 367 3.74 3.71 2.36 14 0.20 Premium E SI2 ... 345 3.79 3.75 2.27 31599 0.20 Ideal E VS2 ... 367 3.76 3.73 2.33 38277 0.21 Premium D VS2 ... 386 3.85 3.81 2.32 1 0.21 Premium E SI1 ... 326 3.89 3.84 2.31 43989 0.21 Premium E SI2 ... 394 3.84 3.82 2.37 38279 0.21 Premium D VS2 ... 386 3.96 3.93 2.30 38278 0.21 Premium D VS2 ... 386 3.89 3.86 2.29 38273 0.21 Very Good E VS2 ... 386 3.82 3.78 2.40 38274 0.21 Premium E VS2 ... 386 3.87 3.83 2.33 38275 0.21 Premium E VS2 ... 386 3.93 3.89 2.33 38276 0.21 Premium D VS2 ... 386 3.82 3.78 2.34 8 0.22 Fair E VS2 ... 337 3.87 3.78 2.49 34611 0.22 Premium F SI1 ... 470 3.90 3.85 2.39 53 0.22 Premium E VS2 ... 404 3.93 3.89 2.41 54 0.22 Premium D VS2 ... 404 3.91 3.88 2.31 12 0.22 Premium F SI1 ... 342 3.88 3.84 2.33 23363 0.23 Ideal E VS2 ... 423 3.88 3.92 2.44 33956 0.23 Very Good E VVS2 ... 465 3.95 4.01 2.43 6720 0.23 Very Good D VS2 ... 577 4.02 4.07 2.43 48985 0.23 Ideal F VVS2 ... 538 3.96 3.99 2.44 ... ... ... ... ... ... ... ... ... 27517 3.01 Good I SI2 ... 18242 9.06 9.01 5.77 27516 3.01 Fair I SI2 ... 18242 8.99 8.94 5.90 27515 3.01 Fair I SI2 ... 18242 8.99 8.94 5.90 26744 3.01 Ideal J I1 ... 16538 8.99 8.93 5.86 25460 3.01 Premium G SI2 ... 14220 9.44 9.37 5.62 27514 3.01 Premium I SI2 ... 18242 9.36 9.31 5.62 27684 3.01 Premium J SI2 ... 18710 9.35 9.22 5.64 26467 3.01 Ideal J SI2 ... 16037 9.25 9.20 5.69 22741 3.01 Fair H I1 ... 10761 9.54 9.38 5.31 27685 3.01 Premium J SI2 ... 18710 9.41 9.32 5.59 19339 3.01 Premium I I1 ... 8040 9.10 8.97 5.67 27649 3.01 Good H SI2 ... 18593 9.44 9.38 5.42 22540 3.02 Fair I I1 ... 10577 9.11 9.02 5.91 26100 3.04 Very Good I SI2 ... 15354 9.14 9.07 5.75 27638 3.04 Premium I SI2 ... 18559 9.51 9.46 5.62 22428 3.05 Premium E I1 ... 10453 9.26 9.25 5.66 21758 3.11 Fair J I1 ... 9823 9.15 9.02 5.98 24297 3.22 Ideal I I1 ... 12545 9.49 9.42 5.92 24131 3.24 Premium H I1 ... 12300 9.44 9.40 5.85 26431 3.40 Fair D I1 ... 15964 9.42 9.34 6.27 24328 3.50 Ideal H I1 ... 12587 9.65 9.59 6.03 27679 3.51 Premium J VS2 ... 18701 9.66 9.63 6.03 23644 3.65 Fair H I1 ... 11668 9.53 9.48 6.38 26534 3.67 Premium I I1 ... 16193 9.86 9.81 6.13 26444 4.00 Very Good I I1 ... 15984 10.01 9.94 6.31 25999 4.01 Premium J I1 ... 15223 10.02 9.94 6.24 25998 4.01 Premium I I1 ... 15223 10.14 10.10 6.17 27130 4.13 Fair H I1 ... 17329 10.00 9.85 6.43 27630 4.50 Fair J I1 ... 18531 10.23 10.16 6.72 27415 5.01 Fair J I1 ... 18018 10.74 10.54 6.98 [53940 rows x 10 columns] Sort the entire diamonds DataFrame by the ‘carat’ Series in descending order carat cut color clarity ... price x y z 27415 5.01 Fair J I1 ... 18018 10.74 10.54 6.98 27630 4.50 Fair J I1 ... 18531 10.23 10.16 6.72 27130 4.13 Fair H I1 ... 17329 10.00 9.85 6.43 25999 4.01 Premium J I1 ... 15223 10.02 9.94 6.24 25998 4.01 Premium I I1 ... 15223 10.14 10.10 6.17 26444 4.00 Very Good I I1 ... 15984 10.01 9.94 6.31 26534 3.67 Premium I I1 ... 16193 9.86 9.81 6.13 23644 3.65 Fair H I1 ... 11668 9.53 9.48 6.38 27679 3.51 Premium J VS2 ... 18701 9.66 9.63 6.03 24328 3.50 Ideal H I1 ... 12587 9.65 9.59 6.03 26431 3.40 Fair D I1 ... 15964 9.42 9.34 6.27 24131 3.24 Premium H I1 ... 12300 9.44 9.40 5.85 24297 3.22 Ideal I I1 ... 12545 9.49 9.42 5.92 21758 3.11 Fair J I1 ... 9823 9.15 9.02 5.98 22428 3.05 Premium E I1 ... 10453 9.26 9.25 5.66 26100 3.04 Very Good I SI2 ... 15354 9.14 9.07 5.75 27638 3.04 Premium I SI2 ... 18559 9.51 9.46 5.62 22540 3.02 Fair I I1 ... 10577 9.11 9.02 5.91 22741 3.01 Fair H I1 ... 10761 9.54 9.38 5.31 27514 3.01 Premium I SI2 ... 18242 9.36 9.31 5.62 27649 3.01 Good H SI2 ... 18593 9.44 9.38 5.42 25460 3.01 Premium G SI2 ... 14220 9.44 9.37 5.62 21862 3.01 Premium F I1 ... 9925 9.24 9.13 5.73 27515 3.01 Fair I SI2 ... 18242 8.99 8.94 5.90 27685 3.01 Premium J SI2 ... 18710 9.41 9.32 5.59 26467 3.01 Ideal J SI2 ... 16037 9.25 9.20 5.69 27684 3.01 Premium J SI2 ... 18710 9.35 9.22 5.64 27517 3.01 Good I SI2 ... 18242 9.06 9.01 5.77 19339 3.01 Premium I I1 ... 8040 9.10 8.97 5.67 27518 3.01 Good I SI2 ... 18242 9.06 9.01 5.77 ... ... ... ... ... ... ... ... ... 37278 0.23 Very Good G VVS1 ... 484 3.95 3.98 2.46 37277 0.23 Very Good G VVS1 ... 484 3.92 3.96 2.42 37276 0.23 Very Good F VS1 ... 483 3.89 3.86 2.45 37604 0.23 Very Good D VVS2 ... 485 3.86 3.89 2.46 54 0.22 Premium D VS2 ... 404 3.91 3.88 2.31 34611 0.22 Premium F SI1 ... 470 3.90 3.85 2.39 8 0.22 Fair E VS2 ... 337 3.87 3.78 2.49 53 0.22 Premium E VS2 ... 404 3.93 3.89 2.41 12 0.22 Premium F SI1 ... 342 3.88 3.84 2.33 38273 0.21 Very Good E VS2 ... 386 3.82 3.78 2.40 38274 0.21 Premium E VS2 ... 386 3.87 3.83 2.33 38275 0.21 Premium E VS2 ... 386 3.93 3.89 2.33 38276 0.21 Premium D VS2 ... 386 3.82 3.78 2.34 38277 0.21 Premium D VS2 ... 386 3.85 3.81 2.32 1 0.21 Premium E SI1 ... 326 3.89 3.84 2.31 38278 0.21 Premium D VS2 ... 386 3.89 3.86 2.29 38279 0.21 Premium D VS2 ... 386 3.96 3.93 2.30 43989 0.21 Premium E SI2 ... 394 3.84 3.82 2.37 31599 0.20 Ideal E VS2 ... 367 3.76 3.73 2.33 31598 0.20 Very Good E VS2 ... 367 3.74 3.71 2.36 31597 0.20 Ideal D VS2 ... 367 3.81 3.77 2.33 31600 0.20 Premium D VS2 ... 367 3.73 3.68 2.31 31595 0.20 Ideal E VS2 ... 367 3.86 3.84 2.30 31594 0.20 Premium E VS2 ... 367 3.84 3.80 2.28 31593 0.20 Premium E VS2 ... 367 3.81 3.78 2.32 31592 0.20 Premium E VS2 ... 367 3.81 3.78 2.24 31591 0.20 Premium E VS2 ... 367 3.79 3.77 2.26 31601 0.20 Premium D VS2 ... 367 3.77 3.72 2.31 14 0.20 Premium E SI2 ... 345 3.79 3.75 2.27 31596 0.20 Premium F VS2 ... 367 3.73 3.71 2.33 [53940 rows x 10 columns]
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a pandas program to sort the 'price' Series in descending order (returns a Series) of diamonds Dataframe.
Next: Write a pandas program to filter the DataFrame rows to only show carat weight at least 0.3.
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-14.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics