Parse Dates and Extract Month
Intermediate · Write the code · from Pandas String and DateTime Operations for Real-World Data
From the lesson: Pandas String and DateTime Operations for Real-World Data
Real-world data is messy. Names arrive in ALL CAPS, dates come as random text strings like "Jan 5, 2024" or "2024/01/05", and phone numbers have dashes in some rows but not others. Before you can analyze anything, you need to clean this stuff up.
Read the lessonConvert the date_str column to datetime, then add a month column with the month number. Print the month column as a list.
import pandas as pd
df = pd.DataFrame({
'event': ['Launch', 'Update', 'Review'],
'date_str': ['2024-03-15', '2024-07-22', '2024-11-01']
})
# Convert date_str to datetime, extract month, print as list
More Pandas String and Datetime exercises
- Clean Messy NamesBeginner
- Filter with str.contains()Intermediate
- Predict the String Operation OutputIntermediate
- Calculate Days Between DatesIntermediate
- Fix the String Extraction BugIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz