Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用padas数据帧中的for、If语句计算持续时间_Python_Pandas_Dataframe_For Loop_If Statement - Fatal编程技术网

Python 使用padas数据帧中的for、If语句计算持续时间

Python 使用padas数据帧中的for、If语句计算持续时间,python,pandas,dataframe,for-loop,if-statement,Python,Pandas,Dataframe,For Loop,If Statement,我是pandas和python的新手-非常感谢您的回答我在数据框中有三列,其中的值如下:print(df) 我试图通过排除暂停和恢复之间的时间来获得在这里花费的总时间 If(name=='Start') a=date&time of that name(start) (storing it in a temp variable) If(name=='Pause') b=date&time of that name(Pause) (storing it in a temp va

我是pandas和python的新手-非常感谢您的回答我在数据框中有三列,其中的值如下:print(df)

我试图通过排除暂停和恢复之间的时间来获得在这里花费的总时间

 If(name=='Start')
a=date&time of that name(start) (storing it in a temp variable)
 If(name=='Pause')
b=date&time of that name(Pause) (storing it in a temp variable)
c=a+b;
 If(name=='resume')
d=date&time of that name(resume) (storing it in a temp variable)
 If(name=='stop')
e=date&time of that name(Stop) (storing it in a temp variable)
f=d+e;
For(time=0)
time=time+(c+f)
想到这个伪代码-有人能帮我吗??
提前感谢。

熊猫数据帧的美妙之处在于,您可以按列和行使用操作,而无需迭代行。只需几行代码即可解决此问题:

确保日期和时间类型为datetime:

df['date&time']=pd.to_datetime(df['date&time'])
然后旋转,使时间成为每辆自行车的列:

df1=df.pivot(index='Id', columns='name' ,values='date&time')
最后,做简单的数学运算:

df1['totaltime']=df1['Stop']-df1['Start']-(df1['Resume']-df1['Pause'])
输出:

name    Pause                   Progress                 Resume                  Start                   Stop                  totaltime
Id                      
Bike1   2021-01-01 17:17:57     2021-01-01 17:19:58     2021-01-01 17:18:50     2021-01-01 17:15:56     2021-01-01 17:20:00     0 days 00:03:11
Bike2   2021-01-01 17:27:57     2021-01-01 17:29:58     2021-01-01 17:28:50     2021-01-01 17:25:56     2021-01-01 17:30:00     0 days 00:03:11

数据框中还有哪些列?具体来说,关键是什么?您如何知道6中的停止与1中的开始相关?我已按列ID更改了ID为的数据帧我可以区分6中的停止与1中的开始相关
name    Pause                   Progress                 Resume                  Start                   Stop                  totaltime
Id                      
Bike1   2021-01-01 17:17:57     2021-01-01 17:19:58     2021-01-01 17:18:50     2021-01-01 17:15:56     2021-01-01 17:20:00     0 days 00:03:11
Bike2   2021-01-01 17:27:57     2021-01-01 17:29:58     2021-01-01 17:28:50     2021-01-01 17:25:56     2021-01-01 17:30:00     0 days 00:03:11