Course Content
Pandas: First Steps
Pandas: First Steps
1. Getting Started
Introduction to PandasSeriesTask: Coffee Orders SeriesTask: Barista's Favorites CoffeesAccessing Elements from a SeriesTask: Drink of the HourTask - Signature DrinksTask: Mid-Morning SpecialsTask - Fetching the Mid-Shift DrinksOperations over SeriesTask: Modifying a single elementTask: Modifying the Entire SeriesTask: Modifying Part of a SeriesTask: Processing the Restock of Inventory
2. Basics of Dataframes
What is a DataFrame?Task: Creating a Table of Popular Coffee DrinksTask: Tracking Coffee Bean DeliveriesTask: Weekly Coffee Sales ReportAdding new ColumnsTask: Finding Total Sales Per DrinkTask: Calculating Total Sales RevenueAdding new RowsTask: Adding a New Employee to the CafΓ© TeamTask: Adding New Coffee Types to the CafΓ© Menu
3. Fetching Data from DataFrames
The `head()` MethodTask: Preview the Coffee MenuThe `tail()` MethodTask: Finding the Most Expensive CoffeesThe `sample()` MethodTask: Randomly Select Customer Orders for Quality CheckExtracting Multiple ColumnsTask: Extracting the Total ProfitTask: Extract Sales Data For AnalysisThe `iloc` PropertyTask: Access Sales Data at CafΓ© Pandasia - Part 1Task: Access Sales Data at CafΓ© Pandasia - Part 2Task: Access Sales Data at CafΓ© Pandasia - Part 3
The `sample()` Method
The sample()
method returns a random row from the DataFrame by default.
There are two optional named parameters which can be used to specify how many random rows to fetch:
n
: Specifies the exact number of random rows to fetch;frac
: Specifies the fraction or percentage of the total rows to fetch. For example,frac=0.2
will return 20% of the total rows;
import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Coffee': ['Espresso', 'Latte', 'Cappuccino', 'Americano', 'Macchiato'], 'Price': [2.5, 3.5, 3.0, 2.8, 3.2], 'Availability': ['Yes', 'Yes', 'Yes', 'Yes', 'No'] }) print("Get 2 random rows from the DataFrame:") random_rows = df.sample(n=2) print(random_rows)
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 5