
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a program in Python to split the date column into day, month, year in multiple columns of a given dataframe
Assume, you have a dataframe and the result for a date, month, year column is,
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
To solve this, we will follow the steps given below −
Solution
Create a list of dates and assign into dataframe.
Apply str.split function inside ‘/’ delimiter to df[‘date’] column. Assign the result to df[[“day”, “month”, “year”]].
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({ 'date': ['17/05/2002','16/02/1990','25/09/1980','11/05/2000','17/09/1986'] }) print("Original DataFrame:") print(df) df[["day", "month", "year"]] = df["date"].str.split("/", expand = True) print("\nNew DataFrame:") print(df)
Output
Original DataFrame: date 0 17/05/2002 1 16/02/1990 2 25/09/1980 3 11/05/2000 4 17/09/1986 New DataFrame: 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
- Related Articles
- Write a program in Python to print the day of the year in a given date series
- How to convert year, month, and day of the month into a complete date in R?
- How to split a string column into multiple columns in R?
- Write a program in Python to transpose the index and columns in a given DataFrame
- Finding day of week from date (day, month, year) in JavaScript
- How to get a Date from year, month and day in Java?
- Write a program in Python Pandas to convert a dataframe Celsius data column into Fahrenheit
- Create date from day, month, year fields in MySQL?
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a program in Python to remove one or more than one columns in a given DataFrame
- Format MySQL date and convert to year-month-day
- Write a Python program to export dataframe into an Excel file with multiple sheets
- How to Convert Numbers to Year/Month/Day or Date in Excel?
- Write a program in Python to covert the datatype of a particular column in a dataframe
- Write a program in Python to find which column has the minimum number of missing values in a given dataframe

Advertisements