简体   繁体   English

Python 和 Pandas:减去和格式化数据框

[英]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.我想在 python 2.x 中减去两个数据帧并将结果格式化为 hh:mm:ss。 My problem is that I am assuming the delta column is a string and it's a number.我的问题是我假设 delta 列是一个字符串并且它是一个数字。 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这是 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 .结果是这样的: 39:49 和 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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