Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to split the date column into day, month, year in multiple columns of a given dataframe
When working with date data in a pandas DataFrame, you often need to split a date column into separate day, month, and year columns. This is useful for analysis, filtering, or creating date-based features.
Problem Statement
Given a DataFrame with a date column in "DD/MM/YYYY" format, we want to extract day, month, and year into separate columns ?
date day month year
0 17/05/2002 17 05 2002
1 16/02/1990 16 02 1990
2 25/09/1980 25 09 1980
3 11/05/2000 11 05 2000
4 17/09/1986 17 09 1986
Using str.split() Method
The most straightforward approach is using pandas str.split() method with the delimiter "/" and expand=True parameter ?
import pandas as pd
# Create a DataFrame with date column
df = pd.DataFrame({
'date': ['17/05/2002', '16/02/1990', '25/09/1980', '11/05/2000', '17/09/1986']
})
print("Original DataFrame:")
print(df)
# Split date column into day, month, year
df[["day", "month", "year"]] = df["date"].str.split("/", expand=True)
print("\nDataFrame after splitting:")
print(df)
Original DataFrame:
date
0 17/05/2002
1 16/02/1990
2 25/09/1980
3 11/05/2000
4 17/09/1986
DataFrame after splitting:
date day month year
0 17/05/2002 17 05 2002
1 16/02/1990 16 02 1990
2 25/09/1980 25 09 1980
3 11/05/2000 11 05 2000
4 17/09/1986 17 09 1986
How It Works
The str.split() method works as follows:
- Delimiter: "/" specifies the character to split on
- expand=True: Returns a DataFrame instead of a Series of lists
- Multiple assignment: Assigns the three resulting columns directly to day, month, year
Alternative: Converting to Datetime
For more robust date handling, you can convert to datetime first ?
import pandas as pd
df = pd.DataFrame({
'date': ['17/05/2002', '16/02/1990', '25/09/1980', '11/05/2000', '17/09/1986']
})
# Convert to datetime first
df['date_dt'] = pd.to_datetime(df['date'], format='%d/%m/%Y')
# Extract components
df['day'] = df['date_dt'].dt.day
df['month'] = df['date_dt'].dt.month
df['year'] = df['date_dt'].dt.year
print(df[['date', 'day', 'month', 'year']])
date day month year
0 17/05/2002 17 5 2002
1 16/02/1990 16 2 1990
2 25/09/1980 25 9 1980
3 11/05/2000 11 5 2000
4 17/09/1986 17 9 1986
Comparison
| Method | Pros | Cons |
|---|---|---|
str.split() |
Simple, preserves leading zeros | No date validation |
to_datetime() |
Validates dates, provides datetime object | Removes leading zeros from numbers |
Conclusion
Use str.split() for simple string-based splitting that preserves formatting. Use to_datetime() when you need proper date validation and further datetime operations.
