0
0
Pandasdata~20 mins

str accessor for string methods in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using str Accessor for String Methods in pandas
📖 Scenario: You work in a small bookstore. You have a list of book titles, but some titles have extra spaces and inconsistent capitalization. You want to clean this list to make it neat and uniform.
🎯 Goal: Clean the book titles by removing extra spaces and making all letters lowercase using pandas str accessor methods.
📋 What You'll Learn
Create a pandas DataFrame with a column named titles containing the exact book titles given.
Create a variable clean_titles that stores the cleaned titles.
Use the str.strip() method to remove extra spaces.
Use the str.lower() method to convert all titles to lowercase.
Print the clean_titles variable to show the cleaned titles.
💡 Why This Matters
🌍 Real World
Cleaning text data is a common task in data science, especially when working with user input, product names, or any text data that may have inconsistent formatting.
💼 Career
Data scientists and analysts often need to preprocess and clean text data before analysis or machine learning. Knowing how to use pandas string methods efficiently helps in preparing clean datasets.
Progress0 / 4 steps
1
Create the DataFrame with book titles
Create a pandas DataFrame called df with one column named titles. The column should have these exact values in this order: ' The Great Gatsby ', 'To Kill a Mockingbird', ' 1984', 'Pride and Prejudice ', 'The Catcher in the Rye'.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where the key is 'titles' and the value is the list of book titles.

2
Create a variable for cleaned titles
Create a variable called clean_titles and set it equal to the titles column from the DataFrame df.
Pandas
Need a hint?

Use df['titles'] to get the titles column and assign it to clean_titles.

3
Clean the titles using str accessor methods
Update the variable clean_titles by applying the str.strip() method to remove extra spaces, then apply the str.lower() method to convert all titles to lowercase.
Pandas
Need a hint?

Chain the string methods using clean_titles.str.strip().str.lower() to clean the text.

4
Print the cleaned titles
Print the variable clean_titles to display the cleaned book titles.
Pandas
Need a hint?

Use print(clean_titles) to show the cleaned titles.