0
0
Data Analysis Pythondata~15 mins

Sample() for random rows in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Sample() to Select Random Rows from Data
📖 Scenario: You work in a small bookstore. You have a list of books with their prices and want to randomly pick some books to check their condition.
🎯 Goal: Learn how to use the sample() method in pandas to select random rows from a dataset.
📋 What You'll Learn
Create a pandas DataFrame with book data
Set a variable for the number of random rows to select
Use the sample() method to pick random rows
Print the selected random rows
💡 Why This Matters
🌍 Real World
Random sampling helps quality check a few items from a large list without checking everything.
💼 Career
Data scientists often use random sampling to explore data or create training and testing sets.
Progress0 / 4 steps
1
Create the books DataFrame
Import pandas as pd and create a DataFrame called books with these exact entries: 'Title' as ['The Alchemist', '1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Moby Dick'], and 'Price' as [10.99, 8.99, 12.5, 9.75, 11.0].
Data Analysis Python
Need a hint?

Use pd.DataFrame() with a dictionary where keys are column names and values are lists of data.

2
Set the number of random rows to select
Create a variable called num_samples and set it to 2 to select two random rows.
Data Analysis Python
Need a hint?

Just assign the number 2 to the variable num_samples.

3
Select random rows using sample()
Use the sample() method on the books DataFrame with the argument n=num_samples and save the result in a variable called random_books.
Data Analysis Python
Need a hint?

Call books.sample(n=num_samples) and assign it to random_books.

4
Print the random rows
Print the variable random_books to display the randomly selected rows.
Data Analysis Python
Need a hint?

Use print(random_books) to show the selected rows. The exact rows will vary because of randomness.