w3resource

Pandas Practice Set-1: Exercises, Practice, Solution


This resource offers a total of 325 Pandas Practice Set-1 problems for practice. It includes 65 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


Diamonds:

This classic dataset contains the prices and other attributes of almost 54,000 diamonds. It's a great dataset for beginners learning to work with data analysis and visualization.

Pandas Dataset Exercises

Content

Column Name Description
price price in US dollars (\$326--\$18,823)
carat weight of the diamond (0.2--5.01)
cut quality of the cut (Fair, Good, Very Good, Premium, Ideal)
color diamond colour, from J (worst) to D (best)
clarity a measurement of how clear the diamond is (I1 (worst), SI2, SI1, VS2, VS1, VVS2, VVS1, IF (best))
x length in mm (0--10.74)
y width in mm (0--58.9)
z depth in mm (0--31.8)
depth total depth percentage = z / mean(x, y) = 2 * z / (x + y) (43--79)
table width of top of diamond relative to widest point (43--95)

Access dimond.csv

import pandas as pd
diamonds = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/diamonds.csv')
print(diamonds)

Source: https://www.kaggle.com/shivam2503/diamonds


1. Read CSV and Print First 5 Rows

Write a Pandas program to read a csv file from a specified source and print the first 5 rows.

Click me to see the sample solution


2. Modify Default Column Values and Print First 6 Rows

Write a Pandas program to read a dataset from diamonds DataFrame and modify the default columns values and print the first 6 rows.

Click me to see the sample solution


3. Select a Series from Diamonds DataFrame

Write a Pandas program to select a series from diamonds DataFrame. Print the content of the series.

Click me to see the sample solution


4. Create a New 'Quality -color' Series

Write a Pandas program to create a new 'Quality -color' Series (use bracket notation to define the Series name) of the diamonds DataFrame.

Click me to see the sample solution


5. Count Rows, Columns, and Data Types

Write a Pandas program to find the number of rows and columns and data type of each column of diamonds Dataframe.

Click me to see the sample solution


6. Summarize Only 'Object' Columns

Write a Pandas program to summarize only 'object' columns of the diamonds Dataframe.

Click me to see the sample solution


7. Rename Two Columns

Write a Pandas program to rename two of the columns of the diamonds Dataframe.

Click me to see the sample solution


8. Rename All Columns

Write a Pandas program to rename all the columns of the diamonds Dataframe.

Click me to see the sample solution


9. Remove the Second Column

Write a Pandas program to remove the second column of the diamonds Dataframe.

Click me to see the sample solution


10. Remove Multiple Columns at Once

Write a Pandas program to remove multiple columns at once of the diamonds Dataframe.

Click me to see the sample solution


11. Remove Multiple Rows at Once

Write a Pandas program to remove multiple rows at once (axis=0 refers to rows) from diamonds dataframe.

Click me to see the sample solution


12. Sort the 'cut' Series in Ascending Order

Write a Pandas program to sort the 'cut' Series in ascending order (returns a Series) of diamonds Dataframe.

Click me to see the sample solution


13. Sort the 'price' Series in Descending Order

Write a Pandas program to sort the 'price' Series in descending order (returns a Series) of diamonds Dataframe.

Click me to see the sample solution


14. Sort Entire DataFrame by 'carat' in Ascending and Descending Order

Write a Pandas program to sort the entire diamonds DataFrame by the 'carat' Series in ascending and descending order.

Click me to see the sample solution


15. Filter Rows with Carat Weight at Least 0.3

Write a Pandas program to filter the DataFrame rows to only show carat weight at least 0.3.

Click me to see the sample solution


16. Convert Python List to Pandas Series

Write a Pandas program to convert a python list to pandas series.

Click me to see the sample solution


17. Find Details of Diamonds Where x > 5, y > 5, and z > 5

Write a Pandas program to find the details of the diamonds where length>5, width>5 and depth>5.

Click me to see the sample solution


18. Find Diamonds That Are Either Premium or Ideal

Write a Pandas program to find the diamonds that are either Premium or Ideal.

Click me to see the sample solution


19. Find Diamonds with a Fair, Good, or Premium Cut

Write a Pandas program to find the diamonds that are with a Fair or Good or Premium.

Click me to see the sample solution


20. Display All Column Labels

Write a Pandas program to display all column labels of diamonds DataFrame.

Click me to see the sample solution


21. Read Only a Subset of 3 Rows

Write a Pandas program to read only a subset of 3 rows from diamonds DataFrame.

Click me to see the sample solution


22. Iterate Through Diamonds DataFrame

Write a Pandas program to iterate through diamonds DataFrame.

Click me to see the sample solution


23. Drop All Non-Numeric Columns

Write a Pandas program to drop all non-numeric columns from diamonds DataFrame.

Click me to see the sample solution


24. Include Only Numeric Columns

Write a Pandas program to include only numeric columns in the diamonds DataFrame.

Click me to see the sample solution


25. Describe Only Certain Data Types

Write a Pandas program to pass a list of data types to only describe certain types of diamonds DataFrame.

Click me to see the sample solution


26. Calculate Mean of Each Numeric Column

Write a Pandas program to calculate the mean of each numeric column of diamonds DataFrame.

Click me to see the sample solution


27. Calculate Mean of Each Row

Write a Pandas program to calculate the mean of each row of diamonds DataFrame.

Click me to see the sample solution


28. Calculate Mean of Price for Each Cut

Write a Pandas program to calculate the mean of price for each cut of diamonds DataFrame.

Click me to see the sample solution


29. Calculate Count, Minimum, and Maximum Price for Each Cut

Write a Pandas program to calculate count, minimum, maximum price for each cut of diamonds DataFrame.

Click me to see the sample solution


30. Create Side-by-Side Bar Plot

Write a Pandas program to create a side-by-side bar plot of the diamonds DataFrame.

Click me to see the sample solution


31. Count Occurrences in 'cut' Series

Write a Pandas program to count how many times each value in cut series of diamonds DataFrame occurs.

Click me to see the sample solution


32. Display Percentages for 'cut' Series Values

Write a Pandas program to display percentages of each value of cut series occurs in diamonds DataFrame.

Click me to see the sample solution


33. Display Unique Values in 'cut' Series

Write a Pandas program to display the unique values in cut series of diamonds DataFrame.

Click me to see the sample solution


34. Count Number of Unique Values in 'cut' Series

Write a Pandas program to count the number of unique values in cut series of diamonds DataFrame.

Click me to see the sample solution


35. Compute Cross-Tabulation of Two Series

Write a Pandas program to compute a cross-tabulation of two Series in diamonds DataFrame.

Click me to see the sample solution


36. Calculate Summary Statistics of 'cut' Series

Write a Pandas program to calculate various summary statistics of cut series of diamonds DataFrame.

Click me to see the sample solution


37. Create a Histogram of the 'carat' Series

Write a Pandas program to create a histogram of the 'carat' Series (distribution of a numerical variable) of diamonds DataFrame.

Click me to see the sample solution


38. Create a Bar Plot of 'value_counts' for 'cut' Series

Write a Pandas program to create a bar plot of the 'value_counts' for the 'cut' series of diamonds DataFrame.

Click me to see the sample solution


39. Create a Boolean DataFrame Indicating Missing Values

Write a Pandas program to create a DataFrame of booleans (True if missing, False if not missing) from diamonds DataFrame.

Click me to see the sample solution


40. Count Missing Values in Each Series

Write a Pandas program to count the number of missing values in each Series of diamonds DataFrame.

Click me to see the sample solution


41. Drop Rows with Any Missing Values and Check Shape

Write a Pandas program to check the number of rows and columns and drop those row if 'any' values are missing in a row of diamonds DataFrame.

Click me to see the sample solution


42. Drop Row if Any or All Values Missing in Two Specific Columns

Write a Pandas program to drop a row if any or all values in a row are missing of diamonds DataFrame on two specific columns.

Click me to see the sample solution


43. Set an Existing Column as the Index

Write a Pandas program to set an existing column as the index of diamonds DataFrame.

Click me to see the sample solution


44. Set Column as Index and Restore the Index Name

Write a Pandas program to set an existing column as the index of diamonds DataFrame and restore the index name, and move the index back to a column.

Click me to see the sample solution


45. Access a Specified Series Index and Values

Write a Pandas program to access a specified Series index and the Series values of diamonds DataFrame.

Click me to see the sample solution


46. Sort a Series by Its Values and Index

Write a Pandas program to sort a Series by its values and index of diamonds DataFrame.

Click me to see the sample solution


47. Calculate the Product of x, y, and z for Each Diamond

Write a Pandas program to calculate the multiply of length, width and depth for each cut of diamonds DataFrame.

Click me to see the sample solution


48. Concatenate Diamonds DataFrame with the 'color' Series

Write a Pandas program to concatenate the diamonds DataFrame with the 'color' Series.

Click me to see the sample solution


49. Read Specified Rows and All Columns

Write a Pandas program to read specified rows and all columns of diamonds DataFrame.

Click me to see the sample solution


50. Read Rows 0, 5, 7 and All Columns

Write a Pandas program to read rows 0, 5, 7 and all columns of diamonds DataFrame.

Click me to see the sample solution


51. Read Rows 2 Through 5 and All Columns

Write a Pandas program to read rows 2 through 5 and all columns of diamonds DataFrame.

Click me to see the sample solution


52. Read Rows 0 Through 2 (Inclusive), Columns 'color' and 'price'

Write a Pandas program to read rows 0 through 2 (inclusive), columns 'color' and 'price' of diamonds DataFrame.

Click me to see the sample solution


53. Read Rows 0 Through 2 (Inclusive), Columns 'color' Through 'price' (Inclusive)

Write a Pandas program to read rows 0 through 2 (inclusive), columns 'color' through 'price' (inclusive) of diamonds DataFrame.

Click me to see the sample solution


54. Read Rows Where 'cut' is 'Premium', Column 'color'

Write a Pandas program to read rows in which the 'cut' is 'Premium', column 'color' of diamonds DataFrame.

Click me to see the sample solution


55. Read Rows in Positions 0 and 1, Columns in Positions 0 and 3

Write a Pandas program to read rows in positions 0 and 1, columns in positions 0 and 3 of diamonds DataFrame.

Click me to see the sample solution


56. Read Rows in Positions 0 Through 4, Columns in Positions 1 Through 4

Write a Pandas program to read rows in positions 0 through 4, columns in positions 1 through 4 of diamonds DataFrame.

Click me to see the sample solution


57. Read Rows in Positions 0 Through 4 (Exclusive) and All Columns

Write a Pandas program to read rows in positions 0 through 4 (exclusive) and all columns of diamonds DataFrame.

Click me to see the sample solution


58. Read Rows 2 Through 5 (Inclusive), Columns in Positions 0 Through 2 (Exclusive)

Write a Pandas program to read rows 2 through 5 (inclusive), columns in positions 0 through 2 (exclusive) of diamonds DataFrame.

Click me to see the sample solution


59. Print Concise Summary of Diamonds DataFrame

Write a Pandas program to print a concise summary of diamonds DataFrame.

Click me to see the sample solution


60. Get True Memory Usage of Diamonds DataFrame

Write a Pandas program to get the true memory usage by diamonds DataFrame.

Click me to see the sample solution


61. Calculate Memory Usage for Each Series in Diamonds DataFrame

Write a Pandas program to calculate the memory usage for each Series (in bytes) of diamonds DataFrame.

Click me to see the sample solution


62. Randomly Sample Rows from Diamonds DataFrame

Write a Pandas program to get randomly sample rows from diamonds DataFrame.

Click me to see the sample solution


63. Sample 75% of Rows Without Replacement and Store 25% Separately

Write a Pandas program to get sample 75% of the diamonds DataFrame's rows without replacement and store the remaining 25% of the rows in another DataFrame.

Click me to see the sample solution


64. Read Diamonds DataFrame and Detect Duplicate 'color'

Write a Pandas program to read the diamonds DataFrame and detect duplicate color.

Note: duplicated () function returns boolean Series denoting duplicate rows, optionally only considering certain columns.

Click me to see the sample solution


65. Count Duplicate Rows in Diamonds DataFrame

Write a Pandas program to count the duplicate rows of diamonds DataFrame.

Click me to see the sample solution


Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

[ Want to contribute to Python Pandas exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.