pandas中关于DataFrame计算时间差(加减)

Dataframe中的时间是不能直接进行相加减的。如果将两列时间进行加减,会弹出类型错误:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

所以需要先用pandas的to_datetime()方法,转化成时间格式进行加减,然后再转换成df格式

new_df = pd.DataFrame(pd.to_datetime(time_df['END_TIME']) - pd.to_datetime(time_df['START_TIME']))

猜你喜欢

转载自blog.csdn.net/weekdawn/article/details/81391808