0
0
Pandasdata~15 mins

Specifying column names and index in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Specifying Column Names and Index in pandas DataFrame
📖 Scenario: You work in a small bookstore. You have sales data but it comes without column names and index labels. You want to organize it properly so you can analyze it easily.
🎯 Goal: Create a pandas DataFrame from raw sales data by specifying the column names and setting a custom index for the rows.
📋 What You'll Learn
Create a pandas DataFrame from a list of lists with raw sales data.
Specify the column names as 'Book', 'Author', and 'Copies Sold'.
Set the index of the DataFrame to be the book titles.
💡 Why This Matters
🌍 Real World
Organizing raw data with proper column names and indexes is essential for clear data analysis and reporting in any business.
💼 Career
Data analysts and scientists often receive data without headers or indexes and must clean and structure it before analysis.
Progress0 / 4 steps
1
Create raw sales data list
Create a variable called raw_data that holds this list of lists exactly: [["The Alchemist", "Paulo Coelho", 120], ["1984", "George Orwell", 85], ["To Kill a Mockingbird", "Harper Lee", 95]]
Pandas
Need a hint?

Use a variable named raw_data and assign the list of lists exactly as shown.

2
Import pandas and define column names
Import pandas as pd. Create a list called columns with these exact strings: 'Book', 'Author', and 'Copies Sold'.
Pandas
Need a hint?

Use import pandas as pd and create a list named columns with the exact column names.

3
Create DataFrame with specified columns
Create a pandas DataFrame called df from raw_data using the columns list to specify the column names.
Pandas
Need a hint?

Use pd.DataFrame() with raw_data and columns=columns.

4
Set the DataFrame index to the book titles
Set the index of the DataFrame df to the 'Book' column using df.set_index('Book', inplace=True).
Pandas
Need a hint?

Use df.set_index('Book', inplace=True) to set the index to the book titles.