简体   繁体   中英

how to extract 2nd and fifth row of data in everyday in pandas?

I have a data frame name df, I want to extract every 2nd and 5th row of the data for every day and put it into a new data frame. How should I do that? The time for my data is already in pd.to_datatime format. 在此处输入图像描述

Thank you.

Use GroupBy.nth by dates extracted by Series.dt.date :

df1 = df.groupby(df['Datetime'].dt.date).nth([2,5])

Or if need original not changed data use GroupBy.cumcount for counter and compare by Series.isin , but subtract 1 because python counts from 0 and filter by boolean indexing :

df1 = df[df.groupby(df['Datetime'].dt.date).cumcount().isin([1,4])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM