Home > Mobile >  Creating multiple dataframes based on date of timestamp in Python
Creating multiple dataframes based on date of timestamp in Python

Time:10-14

I am looking for a elegant solution to create multiple dataframe with mixed up timestamps

I have a dataframe with more than thousand of rows with 16 columns, and one of columns have timestamp value like :

2021-09-28 00:00:00 ~ 2021-09-28 23:59:59

Also, the timestamp value is not continuse on the dataframe, and mixed up the timestamp.

Example

IN [1] : df["datetime"][24082:24085]
OUT [1] :
24082   2021-09-28 07:25:21.446
24083   2021-09-28 07:25:22.444
24084   2021-10-01 19:49:40.549
24085   2021-10-01 19:49:41.549

What I am trying to do is creating multiple dataframes from the dataframe depends on the date of timestamp, such as;

df1 is the rows with 2021-09-28 00:00:00.001 ~ 23:59:59.999
df2 is the rows with 2021-09-29 00:00:00.001 ~ 23:59:59.999
df3 is the rows with 2021-09-30 00:00:00.001 ~ 23:59:59.999
     ...
dfn is the rows with latest date

How can I achieve the abo4

I am looking for a elegant solution output?

CodePudding user response:

Use groupby with Grouper:

for date, data in df.groupby(pd.Grouper(key='datetime', freq='D')):
    # do_something_with_your_data
    print(data)
  • Related