Nowrie
Nowrie

Reputation: 5

How to select rows from random day in a dataframe?

I have a dataframe which has measures every minute from different sensors. I would like to select the measures made in one day, being this day chosen randomly.

This is the dataframe first 10 rows:

                   Time   CO2     H         T
0  21-Dec-2018 15:04:00  1540  59.3  17.95000
1  21-Dec-2018 15:05:00  1440  55.6  18.15000
2  21-Dec-2018 15:06:00  1426  53.7  18.25000
3  21-Dec-2018 15:07:00  1426  52.3  18.35000
4  21-Dec-2018 15:08:00  1382  51.3  18.45000
5  21-Dec-2018 15:09:00  1338  50.3  18.62019
6  21-Dec-2018 15:10:00  1304  49.4  18.75000
7  21-Dec-2018 15:11:00  1274  48.6  18.92019
8  21-Dec-2018 15:12:00  1262  47.8  19.52019
9  21-Dec-2018 15:13:00  1258  47.2  19.22019

For example, if the range of dates goes from '21-Dec-2018 15:04:00' to '31-Dec-2018 23:59:00', randomly select a day, suppose the day 24. After the day is randomly selected get all the measures from that day (They should be 1440 in total, one per minute).

Is this possible?

Upvotes: 0

Views: 372

Answers (1)

yatu
yatu

Reputation: 88226

Start by casting the date column to datetime with pod.to_datetime, and the use DataFrame.sample to take a random sample from the days it contains. Then use it to index the dataframe:

df['Time'] = pd.to_datetime(df.Time)
random_day = df.Time.dt.day.sample(1).values.item()
df_on_random_day = df[df.Time.dt.day.eq(random_day)]

Upvotes: 1

Related Questions