Challenge 2: Band Initials (Trivial)
Explore how to solve a practical data challenge by applying a function to extract the first two letters from band names using Pandas' map method. Understand mapping functions to DataFrame columns and converting the results into a Python dictionary for analysis.
We'll cover the following...
Problem definition
Your music analyst would like to assign nicknames to each band based on the first two letters of their name. Can you help them with that?
Expected output
A python dict, mapping the artist names to the first two letters of their names
Example: {'The Beatles': 'Th', 'Cairokee': 'Ca'}
Challenge
Solution
Solution explanation
Similar to the previous exercise, since you’re applying a function to a specific column (the artist name is in col artist), your desired function will be through df['artist'].map(<function>).
To get the first two characters of a string s, the correct technique would be to write s[:2]. So, the function to be applied is df['artist'].map(lambda x: x[:2]), where x represents the artist name for every row.
The rest is for the formatting of the dict to be returned. So, you will set the index to artist, and select only the new init column containing the name initials. Then, convert this to a dict using .to_dict().