Skip to main content
 首页 » 编程设计

python之将 Pandas 数据框拆分为单独的组

2025年05月04日23Terrylee

给定以下数据集 DF:

uuid,eventTime,Op.progress,Op.progressPercentage, AnotherAttribute 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:52:39,P,3.0,01:57:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:52:49,P,3.0,01:56:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:53:18,P,4.0,01:55:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:53:49,P,5.0,01:55:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:54:27,P,5.0,01:54:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:55:07,P,6.0,01:54:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:55:27,P,6.0,01:53:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:33:46,W,40.0,01:13:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:10,N,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:16,N,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:18,N,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:55,P,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:41:15,P,1.0,01:59:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:41:31,P,3.0,01:57:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:41:51,P,3.0,01:56:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:42:22,P,4.0,01:56:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:42:51,P,4.0,01:55:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:29:22,S,98.0,00:04:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:29:27,S,98.0,00:03:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:30:27,S,99.0,00:02:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:31:27,S,100.0,00:01:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:33:01,F,100.0,00:01:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:33:01,F,100.0,00:01:00 

我想分成两部分:

df1:

uuid,eventTime,Op.progress,Op.progressPercentage, AnotherAttribute 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:52:39,P,3.0,01:57:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:52:49,P,3.0,01:56:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:53:18,P,4.0,01:55:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:53:49,P,5.0,01:55:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:54:27,P,5.0,01:54:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:55:07,P,6.0,01:54:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T12:55:27,P,6.0,01:53:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:33:46,W,40.0,01:13:00 

df2:

uuid,eventTime,Op.progress,Op.progressPercentage, AnotherAttribute 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:10,N,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:16,N,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:18,N,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:40:55,P,1.0,02:00:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:41:15,P,1.0,01:59:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:41:31,P,3.0,01:57:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:41:51,P,3.0,01:56:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:42:22,P,4.0,01:56:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T13:42:51,P,4.0,01:55:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:29:22,S,98.0,00:04:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:29:27,S,98.0,00:03:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:30:27,S,99.0,00:02:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:31:27,S,100.0,00:01:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:33:01,F,100.0,00:01:00 
C0972765-8436-0000-0000-000000000000,2017-08-19T15:33:01,F,100.0,00:01:00 

拆分应基于 Op.progressPercentage 属性,该属性的取值范围为 1 到 100。

当我尝试应用 splitting a pandas Dataframe 提供的解决方案时如下所示,我没有得到正确的预期结果。

df_dataset = pd.read_csv(filepath) #your input data saved here 
wash_list = [] 
shifted = df_dataset['Op.progressPercentage'].shift() 
m = shifted.diff(-1).ne(0) & shifted.eq(100) 
a = m.cumsum() 
aa = df_dataset.groupby([df_dataset.uuid,a]) 
for k, gp in aa:          
    wash_list.append(gp.sort_values(['uuid', 'eventTime'], ascending=[1, 1])) 
 
for wash in wash_list : 
    print("") 
    print(wash.to_string()) 
    print("") 

拜托,我们将不胜感激。 非常感谢您, 此致, 卡罗

请您参考如下方法:

IIUC,(不考虑异常情况)您可以使用 diff + cumsum 来获取不同的组,并在这些组上使用 groupby:

for _, g in df.groupby((~df['Op.progressPercentage']\ 
                          .diff().fillna(0).ge(0)).cumsum()): 
     print(g, '\n') 

详情

组是这样找到的:

(~df['Op.progressPercentage'].diff().fillna(0).ge(0)).cumsum() 
 
0     0 
1     0 
2     0 
3     0 
4     0 
5     0 
6     0 
7     0 
8     1 
9     1 
10    1 
11    1 
12    1 
13    1 
14    1 
15    1 
16    1 
17    1 
18    1 
19    1 
20    1 
21    1 
22    1 
Name: Op.progressPercentage, dtype: int64