Home > Blockchain >  Remove the first day of a dataframe
Remove the first day of a dataframe

Time:08-27

I have the following dataframe:

                         X
Datetime                      
2017-01-01 01:00:00   3129.3460
2017-01-01 02:00:00   5433.4315
2017-01-01 03:00:00   2351.8391
2017-01-01 04:00:00   6788.3210
2017-01-01 05:00:00   1232.8655
                         ...
2022-08-14 20:00:00  8905.5340
2022-08-14 21:00:00  8623.0765
2022-08-14 22:00:00  9054.8312
2022-08-14 23:00:00  10341.4785
2022-08-15 00:00:00  9341.1234

How can i remove the whole day of data, if the first hour of that day is different from zero? In this case, i need to remove the whole 2017-01-01 day.

So i thought of using an if condition with df.drop()

first_data = df.index.min()

if first_data.day != 0:
    df = df.drop(first_data)

But i am not sure what i should pass as argument to df.drop. In the above code, it will only drop the first hour of that first day, since first_data gives me the whole timestamp from years until seconds.

CodePudding user response:

Let's try groupby and filter

out = (df.groupby(df.index.date)
       .filter(lambda g: str(g.index.min().time()) == '00:00:00'))
print(out)

                             X
2022-08-15 00:00:00  9341.1234
2022-08-15 01:00:00  9341.1234
  • Related