R
R
Rinat_Devyatyarov2021-09-15 16:28:05
Python
Rinat_Devyatyarov, 2021-09-15 16:28:05

How to display the difference in date and time in a separate column?

There are tabular data:

table = pd.DataFrame({
    'user': ['Steve', 'Steve', 'Steve', 'Jack', 'Jack', 'Jack'],
    'date': ['02.01.2018', '04.01.2018', '07.01.2018', '02.01.2018', '03.01.2018', '07.01.2018'],
    'time': ['00:05:07', '00:06:14', '00:08:36', '00:14:51', '00:19:18', '00:31:24']
})

I need to calculate the time difference for each user and display it in a separate column in hh:mm:ss format.

I wrote the code:

t1 = pd.to_datetime(0)
for i in table['datetime']: #я объединил колонки date и time, но пропадали секунды
    t2 = pd.to_datetime(i)
    table['difference'] = pd.Timedelta(t2 - t1)
    t1 = t2

Mistake:

:4: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instea

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question