[英]How to select rows from random day in a dataframe?
I have a dataframe which has measures every minute from different sensors.我有一个 dataframe,它每分钟都从不同的传感器进行测量。 I would like to select the measures made in one day, being this day chosen randomly.我想 select 一天采取的措施,这一天是随机选择的。
This is the dataframe first 10 rows:这是 dataframe 前 10 行:
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).例如,如果日期范围从“2018 年 12 月 21 日 15:04:00”到“2018 年 12 月 31 日 23:59:00”,则随机 select 一天,假设第 24 天。随机选择获取当天的所有措施(总共应该是 1440,每分钟一个)。
Is this possible?这可能吗?
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.首先使用DataFrame.sample
pod.to_datetime
它包含的日期中随机抽取样本。 Then use it to index the dataframe:然后用它来索引 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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.