Skip to content Skip to sidebar Skip to footer

How To Add Extra Time Into The Time Column With Data Using Python

Here I have a dataset with date, time and one input column. So here my time column is not good. So I want to give time range into that time column. So here first I did I just conve

Solution 1:

Use:

data['date']= pd.to_datetime(data['date'] + " " + data['time'],
                    format='%d/%m/%Y %H:%M:%S', dayfirst=True)

Subtract firat value of date column and convert to minutes:

data['time1']=data['date'].sub(data.loc[0,'date']).dt.total_seconds()/60print(data)datetimeX3time102018-03-10 06:15:00   6:15:0070.012018-03-10 06:45:00   6:45:00530.022018-03-10 07:45:00   7:45:00790.032018-03-10 09:00:00   9:00:007165.042018-03-10 09:25:00   9:25:007190.052018-03-10 09:30:00   9:30:005195.062018-03-10 11:00:00  11:00:007285.072018-03-10 11:30:00  11:30:007315.082018-03-10 13:30:00  13:30:007435.092018-03-10 13:50:00  13:50:005455.0102018-03-10 15:00:00  15:00:007525.0112018-03-10 15:25:00  15:25:007550.0122018-03-10 16:25:00  16:25:007610.0132018-03-10 18:00:00  18:00:007705.0142018-03-10 19:00:00  19:00:005765.0

Create new 60 range values:

arr = np.arange(0, int(data['time1'].max()), 60)
print (arr)
[  060120180240300360420480540600660720]

Join together with minute column:

union = np.union1d(data['time1'], arr)
print (union)
[  0.30.60.90.120.165.180.190.195.240.285.300.315.360.420.435.455.480.525.540.550.600.610.660.705.720.765.]

Create index by time1 column and DataFrame.reindex - added new values from arr:

data = data.set_index('time1')['X3'].reindex(union, fill_value=0).reset_index()
print (data)
    time1  X3
00.07130.05260.00390.074120.005165.076180.007190.078195.059240.0010285.0711300.0012315.0713360.0014420.0015435.0716455.0517480.0018525.0719540.0020550.0721600.0022610.0723660.0024705.0725720.0026765.05

Post a Comment for "How To Add Extra Time Into The Time Column With Data Using Python"