简体   繁体   中英

Copy certain rows from pandas dataframe to a new one (Time condition)

I have a dataframe which looks like this:

                       pressure mean  pressure std              
2016-03-01 00:00:00     615.686441      0.138287 
2016-03-01 01:00:00     615.555000      0.067460 
2016-03-01 02:00:00     615.220000      0.262840 
2016-03-01 03:00:00     614.993333      0.138841 
2016-03-01 04:00:00     615.075000      0.072778 
2016-03-01 05:00:00     615.513333      0.162049 
................

The first column is the index column.

I want to create a new dataframe with only the rows of 3pm and 3am, so it will look like this:

                       pressure mean  pressure std
2016-03-01 03:00:00     614.993333      0.138841 
2016-03-01 15:00:00     616.613333      0.129493
2016-03-02 03:00:00     615.600000      0.068889
..................

Any ideas ?

Thank you !

I couldn't load your data using pd.read_clipboard() , so I'm going to recreate some data:

df = pd.DataFrame(index=pd.date_range('2016-03-01', freq='H', periods=72),
                  data=np.random.random(size=(72,2)),
                  columns=['pressure', 'mean'])

Now your dataframe should have a DatetimeIndex . If not, you can use df.index = pd.to_datetime(df.index) .

Then its really easy using boolean indexing:

df.ix[(df.index.hour == 3) | (df.index.hour == 15)]

在此处输入图片说明

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