Using map() for Element-wise Transformation in pandas
📖 Scenario: You work in a small bookstore. You have a list of books with their genres. You want to create a new column that shows a short code for each genre to make your data easier to read.
🎯 Goal: Build a pandas DataFrame with book titles and genres, then use map() to create a new column with genre codes.
📋 What You'll Learn
Create a pandas DataFrame called
books with columns 'Title' and 'Genre' using the exact data provided.Create a dictionary called
genre_codes that maps each genre to its short code.Use the
map() function on the 'Genre' column to create a new column called 'GenreCode'.Print the
books DataFrame to show the new column.💡 Why This Matters
🌍 Real World
Mapping long or complex category names to short codes helps in reports, dashboards, and data storage.
💼 Career
Data analysts and scientists often use map() to clean and prepare data for analysis or visualization.
Progress0 / 4 steps