简体   繁体   中英

Python and pandas: subtracting and formatting dataframe

I want to subtract two data frames in python 2.x and format the result to be in hh:mm:ss. My problem is that I am assuming the delta column is a string and it's a number. I need help because I am struggling to make it work. I've searched and tried some solutions found on other posts but I am unable to solve it.

actual= ...select now()

This is the df

        begin                         actual
0  2018-01-31 16:45:04.263      2018-01-31 16:48:06
1  2018-01-31 16:10:26.000      2018-01-31 16:50:06

Now:

df['actual'] = pd.to_datetime(df['actual'])
df['delta'] = df['actual'] - df['begin'] 
df['delta'] = df['delta'].apply(lambda x: str(x)[-8:])

The result it's this: 39:49 and 2.737000 . For the second one I want the same format as for the first. I've tried changing the function like this:

df['delta'] = df['delta'].apply(lambda x: pd.Timedelta(seconds=int(x.total_seconds())))

But it returns :

AttributeError: 'Timestamp' object has no attribute 'total_seconds'

Any ideas would be very appreciated.

I think you need:

print (df.dtypes)
begin     datetime64[ns]
actual    datetime64[ns]
dtype: object


df['delta'] = (df['actual'] - df['begin']).dt.total_seconds()
print (df)
                    begin              actual     delta
0 2018-01-31 16:45:04.263 2018-01-31 16:48:06   181.737
1 2018-01-31 16:10:26.000 2018-01-31 16:50:06  2380.000

If want format it is possible, but a bit crazy (not general solution, because days are removed):

df['delta'] = (df['actual'] - df['begin']).astype(str).str[7:15]
print (df)
                    begin              actual     delta
0 2018-01-31 16:45:04.263 2018-01-31 16:48:06  00:03:01
1 2018-01-31 16:10:26.000 2018-01-31 16:50:06  00:39:40

df['delta'] = (df['actual'] - df['begin']).astype(str)
print (df)
                    begin              actual                      delta
0 2018-01-31 16:45:04.263 2018-01-31 16:48:06  0 days 00:03:01.737000000
1 2018-01-31 16:10:26.000 2018-01-31 16:50:06  0 days 00:39:40.000000000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM