0
0
Data Analysis Pythondata~30 mins

head() and tail() in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Exploring Data with head() and tail()
📖 Scenario: You work as a data analyst for a small bookstore. You have a list of books with their titles, authors, and prices. You want to quickly see the first few and last few books in your list to understand the data better.
🎯 Goal: Learn how to use the head() and tail() methods in pandas to view the first and last rows of a dataset.
📋 What You'll Learn
Create a pandas DataFrame with book data
Use head() to see the first 3 rows
Use tail() to see the last 2 rows
Print the results
💡 Why This Matters
🌍 Real World
Data analysts often need to quickly check the start and end of datasets to understand their structure and spot any issues.
💼 Career
Knowing how to use <code>head()</code> and <code>tail()</code> is a basic but essential skill for data scientists and analysts working with pandas.
Progress0 / 4 steps
1
Create the book data DataFrame
Import pandas as pd and create a DataFrame called books with these exact columns and rows:
Title: 'The Alchemist', '1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Moby Dick'
Author: 'Paulo Coelho', 'George Orwell', 'Harper Lee', 'F. Scott Fitzgerald', 'Herman Melville'
Price: 10.99, 8.99, 7.99, 6.99, 9.99
Data Analysis Python
Need a hint?

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

2
Set the number of rows to view
Create two variables: num_head set to 3 and num_tail set to 2. These will control how many rows to see from the start and end of the DataFrame.
Data Analysis Python
Need a hint?

Just assign the numbers 3 and 2 to the variables num_head and num_tail.

3
Use head() and tail() to get rows
Create two new variables: first_books using books.head(num_head) and last_books using books.tail(num_tail) to get the first and last rows.
Data Analysis Python
Need a hint?

Use head() and tail() methods with the variables num_head and num_tail.

4
Print the first and last books
Print the variables first_books and last_books to see the first 3 and last 2 rows of the DataFrame.
Data Analysis Python
Need a hint?

Use print() to show the DataFrames first_books and last_books.