I have a data frame that looks like this:
price
Date
2022-01-01 19:20:00 100
2022-01-01 19:27:00 100
2022-01-02 19:31:00 102
I want the dataframe to only have unique dates:
price
Date
2022-01-01 19:20:00 100
2022-01-02 19:31:00 102
How can I achieve that?
CodePudding user response:
You can sort the dataframe with:
df = df.sort_values('Date')
And than leave only the rows with a new date with:
df = df[df['Date'].dt.date != df['Date'].shift().dt.date]
CodePudding user response:
You can extract the date from the datetime column using df.Date.dt.date, put that into a new column using assign, and after that use drop_duplicates based on only that column. Last, you might want to drop the newly create column that has only the date information. In code that reads
df = (
df.assign(new_date=lambda df:df.Date.dt.date)
.drop_duplicates(subset=["new_date"])
.drop(columns=["new_date"])
)
