0

I would like to create a new column based in another one, but I'd like to ignore the first value and start copying from the second value, in order to create a new column where I substract previous values. I'm trying to create this code to compare two datetimes and check how much time takes between these 2 values (which are datetimes and will be different in seconds/minutes).

I.E. code:

Column1 New_column    Substraction
18:30   18:45         18:45 - 18:30 = 0:15
18:45   19:45         19:45 - 18:45 = 1:00
19:45   20:15         20:15 - 19:45 = 0:30
20:15   etc.

I'd say it's basically copy Column1 to another one but moving indexes, the first index of second column (index 0) has to be the second index of first column (index 1). Sorry if I don't explain myself very well, I'm trying to improve my English.

Thanks in advance!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
diazze
  • 21
  • 3

2 Answers2

0

you can use pandas.DataFrame.diff to substract previous values as you define in your question

Example:

>>> import pandas as pd
>>> df =  df = pd.DataFrame({'a': [1, 2, 3, 4, 5, 6],'b':[6,5,4,3,2,1]})
>>> df
   a  b
0  1  6
1  2  5
2  3  4
3  4  3
4  5  2
5  6  1
>>> df.a.diff()
0    NaN
1    1.0
2    1.0
3    1.0
4    1.0
5    1.0
Name: a, dtype: float64
Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
0

Convert Column to datetime and find difference of next column(shift)

df['Column1'] = pd.to_datetime(df['Column1'], format='%H:%M')
df['substaction'] = df['Column1'].shift(-1) - df['Column1']
df['Column1'] = df['Column1'].dt.strftime('%H:%M')
df

which gives you the following output

    Column1     substaction
0   18:30   00:15:00
1   18:45   01:00:00
2   19:45   00:30:00
3   20:15   NaT
Prince Francis
  • 2,995
  • 1
  • 14
  • 22