I have a date column in pandas data frame as following
Date | Department Cash flow |
---|---|
Friday, 1 April 2022 | 1550 |
Thursday, 26 August 2021 | 2550 |
Wednesday, 9 September 2020 | 1551 |
I want to remove the days on the left of actual dates including the comma as in the Date column so that it looks as in
Date | Department Cash flow |
---|---|
1 April 2022 | 1550 |
26 August 2021 | 2550 |
9 September 2020 | 1551 |
This will help me organise the data as per the chronology in the dates.
I appreciate your kind suggestions.
CodePudding user response:
It depends on the data type of your date. Is it a string or a datetime format?
If its a string you can use slicing methods, otherwise you can use the datetime library to stringify your date and then slice it.
CodePudding user response:
You can use sting function in pandas
import pandas as pd
# Data
df = pd.DataFrame({
"Date": [
"Friday, 1 April 2022",
"Thursday, 26 August 2021",
"Wednesday, 9 September 2020"]})
# Arrange Dates
df["Date"] = df["Date"].str.split(",")\
.str[-1].str.rstrip().str.lstrip()
I added rstrip
and lstrip
to remove heading and trailing whitespaces.