计算两个 Pandas 列之间的时间差(以小时和分钟为单位)
在 Pandas 中处理基于时间的数据时,通常需要计算两个日期或日期时间列之间的差异。默认情况下,此计算返回一个 datetime.timedelta 对象,其中包括天、小时、分钟和秒。然而,在某些场景下,您可能只想显示小时和分钟。
为了实现这一点,我们可以利用 Pandas 提供的 as_type 方法。操作方法如下:
import pandas as pd import numpy as np # Create a DataFrame with 'todate' and 'fromdate' columns data = {'todate': pd.to_datetime(['2014-01-24 13:03:12.050000', '2014-01-27 11:57:18.240000', '2014-01-23 10:07:47.660000']), 'fromdate': pd.to_datetime(['2014-01-26 23:41:21.870000', '2014-01-27 15:38:22.540000', '2014-01-23 18:50:41.420000'])} df = pd.DataFrame(data) # Calculate the difference between 'todate' and 'fromdate' df['diff'] = df['fromdate'] - df['todate'] # Convert the 'diff' column to hours and minutes df['diff'] = df['diff'].astype(np.timedelta64, copy=False)
通过将 diff 列转换为精度为小时的 timedelta64 对象,我们忽略天数部分,仅保留小时和分钟。
输出:
todate fromdate diff 0 2014-01-24 13:03:12.050 2014-01-26 23:41:21.870 58 hours 0 minutes 1 2014-01-27 11:57:18.240 2014-01-27 15:38:22.540 3 hours 41 minutes 2 2014-01-23 10:07:47.660 2014-01-23 18:50:41.420 8 hours 42 minutes
以上是如何计算两个 Pandas 列之间的时差(以小时和分钟为单位)?的详细内容。更多信息请关注PHP中文网其他相关文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver CS6
视觉化网页开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。