Calculate Days Between Dates
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 lessonAdd a column days_to_deliver that shows how many days passed between order_date and delivery_date. Print the column as a list of integers.
import pandas as pd
df = pd.DataFrame({
'order_date': pd.to_datetime(['2024-01-01', '2024-02-10', '2024-03-20']),
'delivery_date': pd.to_datetime(['2024-01-04', '2024-02-15', '2024-03-22'])
})
# Calculate days between order and delivery
More Pandas String and Datetime exercises
- Clean Messy NamesBeginner
- Filter with str.contains()Intermediate
- Predict the String Operation OutputIntermediate
- Parse Dates and Extract MonthIntermediate
- Fix the String Extraction BugIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz