📖 Scenario: Imagine you work in a small bookstore. You have a list of books with details like title, author, and price. You want to understand how many books you have and how many details are recorded for each book.
🎯 Goal: You will create a pandas DataFrame with book details and then find out its shape, which tells you the number of rows and columns.
📋 What You'll Learn
Create a pandas DataFrame with exact book data
Create a variable to store the DataFrame shape
Use the shape attribute to get dimensions
Print the shape to see rows and columns
💡 Why This Matters
🌍 Real World
Knowing the shape of data helps you understand how much information you have and how it is organized, which is important before analysis.
💼 Career
Data scientists and analysts often check data dimensions to plan cleaning, visualization, and modeling steps.
Progress0 / 4 steps
1
Create the DataFrame with book details
Import pandas as pd and create a DataFrame called books with these exact entries: columns 'Title', 'Author', 'Price' and rows: 'The Alchemist', 'Paulo Coelho', 10.99, '1984', 'George Orwell', 8.99, 'Python 101', 'Michael Driscoll', 15.50.
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column data.
2
Create a variable to store the DataFrame shape
Create a variable called dimensions and set it to the shape of the books DataFrame using the shape attribute.
Pandas
Hint
Use books.shape to get a tuple with rows and columns.
3
Extract number of rows and columns
Create two variables called num_rows and num_columns. Set num_rows to the first value of dimensions and num_columns to the second value of dimensions.
Pandas
Hint
The shape attribute returns a tuple: first is rows, second is columns.
4
Print the number of rows and columns
Print the values of num_rows and num_columns separated by a space.
Pandas
Hint
Use print(num_rows, num_columns) to show the shape.
Practice
(1/5)
1. What does the shape attribute of a pandas DataFrame tell you?
easy
A. The data types of each column
B. The summary statistics of the data
C. The first few rows of the DataFrame
D. The number of rows and columns as a tuple (rows, columns)
Solution
Step 1: Understand what shape returns
The shape attribute gives the size of the DataFrame as a tuple showing rows and columns.
Step 2: Compare options with this definition
Only The number of rows and columns as a tuple (rows, columns) correctly describes shape as (rows, columns).
Final Answer:
The number of rows and columns as a tuple (rows, columns) -> Option D