Gopal Chitalia
Gopal Chitalia

Reputation: 462

how to extract every data point of a minute in a dataset?

I have a dataset which contains every minute data for one month. So, how do I get every data-point for a particular minute?

My Pandas Dataframe looks some thing like this:

  1. 2018-06-01 08:00:00
  2. 2018-06-01 08:01:00
  3. 2018-06-01 08:02:00
  4. ............
  5. ............
  6. ............
  7. 2018-06-30 23:57:00
  8. 2018-06-30 23:58:00
  9. 2018-06-30 23:59:00

Example: Let's say I want to get every data point for 08:03:00. So there will be 30 instances of that value in the whole month. So, how do I get all the values for that minute?

If anyone has worked on something like this, it would be really helpful.

Edit: My dataframe is like this

Dataframe description

Upvotes: 1

Views: 472

Answers (2)

jezrael
jezrael

Reputation: 863651

Create DatetimeIndex and then use DataFrame.at_time:

df = pd.DataFrame({'Ambient': [1,5,6]},
                   index= ['2018-06-01 08:00:00','2018-06-01 08:01:00','2018-06-01 08:01:00'])
print (df)
                     Ambient
2018-06-01 08:00:00        1
2018-06-01 08:01:00        5
2018-06-01 08:01:00        6

#if necessary
df.index = pd.to_datetime(df.index)

df1 = df.at_time('08:01:00')
print (df1)
                     Ambient
2018-06-01 08:01:00        5
2018-06-01 08:01:00        6

Upvotes: 4

Sociopath
Sociopath

Reputation: 13426

Here's one solution:

import pandas as pd
df = pd.DataFrame({'datetime':['2018-06-01 08:00:00', '2018-06-01 08:01:00', '2018-06-01 08:01:00']})

# Convert datetime column into `pandas datetime`
df['datetime'] = pd.to_datetime(df['datetime'])

# Create a new column with only time
df['tme'] = df['datetime'].dt.time

# This is a time for which you have to fetch the data
t = pd.to_datetime('08:01:00').time()

print(df[df['tme']==t])

Output:

    datetime             tme
1   2018-06-01 08:01:00  08:01:00
2   2018-06-01 08:01:00  08:01:00

Upvotes: 2

Related Questions