Home > front end >  How to select the rows in a pandas dataframe which consists of data between two given hours
How to select the rows in a pandas dataframe which consists of data between two given hours

Time:05-13

I have a pandas dataframe df as given below which consists of human readable timestamps in the column time_column. I have extracted just the time HH:MM:SS from the time column into the extracted_time column. My requirement is that I need to create a new column in the dataframe which which will consist of a value of 1 if the time lies between 00:00:00 Hrs to 06:00:00 Hrs, a value of 2 if the time lies between 06:00:00 Hrs to 12:00:00 Hrs, a value of 3 if the time lies between 12:00:00 Hrs to 18:00:00 Hrs, and a value of 4 if the time lies between 18:00:00 Hrs to 00:00:00 Hrs. How can that be done?

enter image description here

CodePudding user response:

First you can convert the datetime column to string and then apply the conditions.

df['hours'] = pd.to_datetime(df['Time']).dt.time     
df['hours']= df['hours'].astype(str)

def condition(x):
    if '00:00:00' <= x < '06:00:00':
        return '1'
    elif '06:00:00' <= x < '12:00:00':
        return '2'
    elif '12:00:00' <= x < '18:00:00':
        return '3'
    else:
        return '4'

df['interval'] = df["hours"].apply(condition)

CodePudding user response:

Seems only the hour is needed to answer the question :

df["extracted_hour"] = pd.to_datetime(df["time_column"]).dt.hour

df["day_fraction"] = np.nan

df.loc[df["extracted_hour"] < 6, "day_fraction"] = 1
df.loc[(df["extracted_hour"] >= 6) & (df["extracted_hour"] < 12), "day_fraction"] = 2
df.loc[(df["extracted_hour"] >= 12) & (df["extracted_hour"] < 18), "day_fraction"] = 3
df.loc[(df["extracted_hour"] >= 18) & (df["extracted_hour"] < 24), "day_fraction"] = 4
df["day_fraction"] = df["day_fraction"].astype(int)
  • Related