Spliting pandas dataframe by date

Question:

I have dataset df with dates:

tpep_dropoff_datetime

2020-01-01 00:33:03 (first date)    
2020-02-01 00:16:35 (last date) 

Desired output should look like this:

tpep_dropoff_datetime

2020-01-01
2020-01-02      
2020-01-03
2020-01-04
...

Is there a way to split this dataframe to seperate ones by this field. Thanks.

Asked By: DaBa

||

Answers:

Difficult to give a complete answer with so few details.

If you really want to split you can use:

dfs = [g for _,g in df.groupby(pd.to_datetime(df['tpep_dropoff_datetime']).dt.normalize())]

Or as a dictionary with the date as key:

dfs = {date: g for date, g in df.groupby(pd.to_datetime(df['tpep_dropoff_datetime']).dt.normalize().astype(str))}

as CSV files

for date, g in df.groupby(pd.to_datetime(df['tpep_dropoff_datetime']).dt.normalize().astype(str)):
    g.to_csv(f'{date}.csv')
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.