如何在python数据帧中对具有相同时间秒的行进行分组?

时间:2018-03-27 15:48:41

标签: python dataframe

我正在处理一个包含一列时间的数据框(hh:mm:ss.zzz),我正在寻找一个解决方案,将所有元素组合在一起,具有相似的秒数。

1 个答案:

答案 0 :(得分:1)

如果不发布您的DataFrame,您的问题很难回答,但这可能就是您所追求的:

import pandas as pd
import re

df = pd.DataFrame([['ID1','01:22:52.134'],['ID2','03:21:31.123'],['ID3','21:12:52.544'],['ID4','23:12:31.216'],['ID5','10:22:02.134'],['ID6','06:52:46.184']], columns=["ID's",'Time'])

pattern = re.compile(r':(\d{2})\.')

dfout = df["ID's"].groupby(df['Time'].str.extract(pattern, expand=False))

for name, group in dfout:
    print('Group Name: ' + name)
    print(group)

收率:

Group Name: 02
4    ID5
Name: ID's, dtype: object
Group Name: 31
1    ID2
3    ID4
Name: ID's, dtype: object
Group Name: 46
5    ID6
Name: ID's, dtype: object
Group Name: 52
0    ID1
2    ID3
Name: ID's, dtype: object