Working with dates in Python is a common requirement for data manipulation and analysis. One of the most useful libraries for this purpose is pandas, which provides various functions to manipulate and process data in a structured way, such as DataFrames.
One task you might encounter while working with dates in a pandas DataFrame is the need to change the date format. This tutorial will show you how to change the date format in a Python DataFrame step by step.
Step 1: Importing libraries and creating a sample DataFrame
First, we need to import the necessary libraries – pandas and datetime – and create a simple DataFrame with a date column to demonstrate the process.
1 2 3 4 5 6 7 8 9 10 |
import pandas as pd import datetime data = { 'name': ['John', 'Jane', 'Sam', 'Sara'], 'birth_date': ['1990-01-30', '1992-06-15', '1988-12-25', '1995-10-05'] } df = pd.DataFrame(data) print(df) |
Output:
name birth_date 0 John 1990-01-30 1 Jane 1992-06-15 2 Sam 1988-12-25 3 Sara 1995-10-05
In this example, we assume that the date format in the ‘birth_date’ column is ‘YYYY-MM-DD.’
Step 2: Convert ‘birth_date’ column to datetime objects
Next, we will convert the ‘birth_date’ column from string objects to datetime objects. This step is essential because you cannot directly change the date format of a string object. By converting the dates to datetime objects, you can use various datetime functions to manipulate the date format.
1 2 |
df['birth_date'] = pd.to_datetime(df['birth_date']) print(df) |
Output:
name birth_date 0 John 1990-01-30 1 Jane 1992-06-15 2 Sam 1988-12-25 3 Sara 1995-10-05
Now, the ‘birth_date’ column contains datetime objects with the default format (‘YYYY-MM-DD’).
Step 3: Change the date format
To change the date format in the ‘birth_date’ column, we will use the apply() and strftime() functions. The apply() function enables you to apply a specific function to the dataframe. Meanwhile, the strftime() function is a method available in datetime objects to format the datetime as a string in a specified format.
Let’s change the date format to ‘DD-MM-YYYY.’
1 2 |
df['birth_date'] = df['birth_date'].apply(lambda x: x.strftime('%d-%m-%Y')) print(df) |
Output:
name birth_date 0 John 30-01-1990 1 Jane 15-06-1992 2 Sam 25-12-1988 3 Sara 05-10-1995
Now the ‘birth_date’ column has the desired date format (‘DD-MM-YYYY’).
Full code
The complete code for changing the date format in a Python DataFrame is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import pandas as pd import datetime data = { 'name': ['John', 'Jane', 'Sam', 'Sara'], 'birth_date': ['1990-01-30', '1992-06-15', '1988-12-25', '1995-10-05'] } df = pd.DataFrame(data) print("Original DataFrame:") print(df) print() df['birth_date'] = pd.to_datetime(df['birth_date']) print("DataFrame after converting 'birth_date' to datetime objects:") print(df) print() df['birth_date'] = df['birth_date'].apply(lambda x: x.strftime('%d-%m-%Y')) print("DataFrame after changing the date format in 'birth_date':") print(df) |
Output:
Original DataFrame: name birth_date 0 John 1990-01-30 1 Jane 1992-06-15 2 Sam 1988-12-25 3 Sara 1995-10-05 DataFrame after converting 'birth_date' to datetime objects: name birth_date 0 John 1990-01-30 1 Jane 1992-06-15 2 Sam 1988-12-25 3 Sara 1995-10-05 DataFrame after changing the date format in 'birth_date': name birth_date 0 John 30-01-1990 1 Jane 15-06-1992 2 Sam 25-12-1988 3 Sara 05-10-1995
Conclusion
In this tutorial, you learned how to change the date format in a pandas DataFrame. By using datetime objects and the apply() and strftime() functions, you can quickly and easily change the date format as required. Now you’re ready to perform more advanced date manipulations and analyses in your own data projects!