2

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 !

ValientProcess
  • 1,699
  • 5
  • 27
  • 43

1 Answers1

3

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)]

enter image description here

Julien Marrec
  • 11,605
  • 4
  • 46
  • 63
  • Seems ix is deprecated. Is there any other new API to replace this? – Mahendran Feb 20 '18 at 11:18
  • in this specific case, `.loc` will do the same. You can refer to [this question](https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation) for more info on ix, iloc versus loc. – Julien Marrec Feb 20 '18 at 15:52