Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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中的数据帧结构_Python_Pandas_Dataframe - Fatal编程技术网

更改Python中的数据帧结构

更改Python中的数据帧结构,python,pandas,dataframe,Python,Pandas,Dataframe,我的Python数据框架如下所示: BatchID | EventTime Test1 | 2020-03-10 10:22:21 Test1 | 2020-03-10 10:22:27 Test1 | 2020-03-10 10:22:31 Test2 | 2020-03-10 10:36:00 Test2 | 2020-03-10 10:36:02 Test2 | 2020-03-10 10:36:04 我想对其进行重组,使其看起来如下所示: Batch

我的Python数据框架如下所示:

BatchID | EventTime

Test1   | 2020-03-10 10:22:21

Test1   | 2020-03-10 10:22:27

Test1   | 2020-03-10 10:22:31

Test2   | 2020-03-10 10:36:00

Test2   | 2020-03-10 10:36:02

Test2   | 2020-03-10 10:36:04
我想对其进行重组,使其看起来如下所示:

BatchID --> Batch Start --> Batch End

Test1  --> 2020-03-10 10:22:21 --> 2020-03-10 10:22:31
Test2  --> 2020-03-10 10:36:00 --> 2020-03-10 10:36:04

其中,对于每个唯一的批次ID,我希望选择开始时间和结束时间,并将其框成一行。

使用
groupby
agg
min,max

df.groupby('BatchID')['EventTime'].agg(['min', 'max']).reset_index()
  BatchID                 min                 max
0   Test1 2020-03-10 10:22:21 2020-03-10 10:22:31
1   Test2 2020-03-10 10:36:00 2020-03-10 10:36:04