Anony___
Anony___

Reputation: 55

How to extract 1 hour data from python dataframe?

I have a dataframe which consist of Length & Time , here I'm attaching a sample dataframe, I was trying to fetch 1 hour data from this dataframe, can you help me to fetch 1 hour data, (if you have ideas to extract data, please let me know)

Length,Time
0.0,2019-08-26 14:46:36.040
0.0,2019-08-26 14:46:36.043
0.0,2019-08-26 14:56:40.156
0.0,2019-08-26 14:56:40.160
6033.0,2019-08-26 15:01:22.963
6033.0,2019-08-26 15:01:23.034
0.0,2019-08-26 15:01:32.759
0.0,2019-08-26 15:01:32.763
0.0,2019-08-26 16:05:13.365
0.0,2019-08-26 16:05:13.368
0.0,2019-08-26 16:12:08.760
0.0,2019-08-26 16:12:08.760
2658.0,2019-08-26 16:14:48.129
2658.0,2019-08-26 17:14:48.132
0.0,2019-08-26 17:22:49.358
0.0,2019-08-26 17:22:49.361
0.0,2019-08-26 17:22:50.152
0.0,2019-08-26 17:22:50.156
0.0,2019-08-26 17:23:08.735
0.0,2019-08-26 18:23:08.735
0.0,2019-08-26 18:23:08.738
0.0,2019-08-26 18:23:08.738

Thank you

Upvotes: 1

Views: 938

Answers (2)

jezrael
jezrael

Reputation: 862661

You can filter maximal hour per data in boolean indexing:

h = df['Time'].dt.hour
df = df[h.eq(h.max())]
print (df)
    Length                    Time
19     0.0 2019-08-26 18:23:08.735
20     0.0 2019-08-26 18:23:08.738
21     0.0 2019-08-26 18:23:08.738

Upvotes: 1

Dishin H Goyani
Dishin H Goyani

Reputation: 7693

Set Time as datetime index then you select rows by hours like

df = pd.read_csv('d1.csv')
df.Time = pd.to_datetime(df.Time)
df.set_index('Time',inplace=True)

df.loc['2019-08-26 14']
                         Length
Time
2019-08-26 14:46:36.040     0.0
2019-08-26 14:46:36.043     0.0
2019-08-26 14:56:40.156     0.0
2019-08-26 14:56:40.160     0.0

df.loc['2019-08-26 15']
                         Length
Time
2019-08-26 15:01:22.963  6033.0
2019-08-26 15:01:23.034  6033.0
2019-08-26 15:01:32.759     0.0
2019-08-26 15:01:32.763     0.0

Upvotes: 0

Related Questions