0

I have a Python Dataframe that looks like this:

                        Facility   PUE   PUEraw   Servers
2016-11-14 00:00:00     6.0        NaN   1.2      5.0 
2016-11-14 00:30:00     6.0        NaN   1.2      5.0 
2016-11-14 01:00:00     6.0        NaN   1.2      5.0 

etc.

As you can see, the index is date/time. The dataframe is updated with a new value every half hour.

I'm trying to write a script that removes all rows except those that correspond to TODAY's date, for which I am utilising date = dt.datetime.today(). However, I am struggling, partly perhaps because the index also contains the time.

Does anyone have any suggestions? Alternatively, a script that removes all but the last 48 rows would also work for me (the last 48 x half hourly values = the latest day's data).

2 Answers 2

2

Here are two options you can use to extract data on a specific day:

df['2016-11-16']
#                     Facility  PUE  PUEraw  Servers
# 2016-11-16 01:00:00      6.0  NaN     1.2      5.0

import datetime
df[df.index.date == datetime.datetime.today().date()]
#                     Facility  PUE  PUEraw  Servers
# 2016-11-16 01:00:00      6.0  NaN     1.2      5.0
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I haven't tried this as I've gone with the other method, but it looks good to me.
1

You can always access the last rows in a DataFrame with df.tail()

df = df.tail(48)

For further information:

Pandas Documentation

1 Comment

Thanks. This works perfectly and is nice and simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.