Home > Software design >  How to find the number of seconds elapsed from the start of the day in pandas dataframe
How to find the number of seconds elapsed from the start of the day in pandas dataframe

Time:05-14

I have a pandas dataframe df in which I have a column named time_column which consists of timestamp objects. I want to calculate the number of seconds elapsed from the start of the day i.e from 00:00:00 Hrs for each timestamp. How can that be done?

enter image description here

CodePudding user response:

You can use pandas.Series.dt.total_seconds

df['time_column'] = pd.to_datetime(df['time_column'])
df['second'] = pd.to_timedelta(df['time_column'].dt.time.astype(str)).dt.total_seconds()

CodePudding user response:

Do df['time_column]. That will give you the time column. Than just do something like:

 import datetime as date
 current_date = date.datetime.now()
 time_elapsed = []
 for x in range(0, current_date.minute*60   current_date.hour*60*60):
     time_elapsed.append((df['time_column'][x].minute*60   df['time_column][x].hour*60*60)- (current_date.minute*60   current_date.hour*60*60))
  • Related