Home > other >  Spliting pandas dataframe by date
Spliting pandas dataframe by date

Time:11-05

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.

CodePudding user response:

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')
  • Related