Pandas数据帧使用groupby对子集进行反向排序

时间:2017-01-04 22:45:08

标签: python sorting pandas group-by

我有一个names = ['id','t','metric_1','metric_2','metric_3']的数据框。我正在每个grp in groupby('id')上运行一些信号处理。我需要扭转整个数据框中的另一个进程的时间,并在引擎盖下进行处理。简单地说,给定grp,我只需要反转时间列,保留所有其他列的完整性,并且grp内的所有行都不完整。

输入数据帧:

    id  t   metric_1    metric_2    metric_3
0   0   86  13.333  61.989  0.017444
1   0   87  13.333  61.993  0.017569
2   0   88  13.333  61.992  0.017711
3   0   89  13.333  61.998  0.017746
4   0   90  13.333  61.993  0.017871
5   1   32  13.333  61.964  0.018511
6   1   33  20.000  61.913  0.020058
7   1   34  20.000  61.864  0.022475
8   1   35  26.667  61.802  0.025995
9   1   36  33.123  61.563  0.032345
10  1   37  33.763  61.836  0.060189
11  2   2   13.333  61.964  0.018511
12  2   3   20.000  61.613  0.020058
13  2   4   20.000  61.164  0.027475
14  2   5   26.667  61.802  0.024995
15  2   6   33.333  61.736  0.030689

我想使用产生如下数据帧的操作:

    id  t   metric_1    metric_2    metric_3
0   0   90  13.333  61.989  0.017444
1   0   89  13.333  61.993  0.017569
2   0   88  13.333  61.992  0.017711
3   0   87  13.333  61.998  0.017746
4   0   86  13.333  61.993  0.017871
5   1   37  13.333  61.964  0.018511
6   1   36  20.000  61.913  0.020058
7   1   35  20.000  61.864  0.022475
8   1   34  26.667  61.802  0.025995
9   1   33  33.333  61.736  0.030689
10  1   32  33.763  61.836  0.060189
11  2   6   13.333  61.964  0.018511
12  2   5   20.000  61.613  0.020058
13  2   4   20.000  61.164  0.027475
14  2   3   26.667  61.802  0.024995
15  2   2   33.333  61.736  0.030689

2 个答案:

答案 0 :(得分:4)

UPDATE2:排序/替换t列中的值,但仅适用于id == 0as described in this comment)的行:

In [373]: df
Out[373]:
   id   t  metric_1  metric_2  metric_3
0   0  86    13.333    61.989  0.017444
1   0  87    13.333    61.993  0.017569
2   0  88    13.333    61.992  0.017711
3   0  89    13.333    61.998  0.017746
4   0  90    13.333    61.993  0.017871
5   1  86    13.333    61.964  0.018511
6   1  87    20.000    61.913  0.020058
7   1  88    20.000    61.864  0.022475
8   1  89    26.667    61.802  0.025995
9   1  90    33.333    61.736  0.030689

In [374]: df.loc[df.id == 0, 't'] = df.loc[df.id == 0, 't'].sort_values(ascending=0).values

In [375]: df
Out[375]:
   id   t  metric_1  metric_2  metric_3
0   0  90    13.333    61.989  0.017444
1   0  89    13.333    61.993  0.017569
2   0  88    13.333    61.992  0.017711
3   0  87    13.333    61.998  0.017746
4   0  86    13.333    61.993  0.017871
5   1  86    13.333    61.964  0.018511
6   1  87    20.000    61.913  0.020058
7   1  88    20.000    61.864  0.022475
8   1  89    26.667    61.802  0.025995
9   1  90    33.333    61.736  0.030689
更新数据集的

更新

原创DF:

In [363]: df
Out[363]:
   id   t  metric_1  metric_2  metric_3
0   0  86    13.333    61.989  0.017444
1   0  87    13.333    61.993  0.017569
2   0  88    13.333    61.992  0.017711
3   0  89    13.333    61.998  0.017746
4   0  90    13.333    61.993  0.017871
5   1  86    13.333    61.964  0.018511
6   1  87    20.000    61.913  0.020058
7   1  88    20.000    61.864  0.022475
8   1  89    26.667    61.802  0.025995
9   1  90    33.333    61.736  0.030689

排序完整的行:

In [364]: df.sort_values(['id','t'], ascending=[1,0])
Out[364]:
   id   t  metric_1  metric_2  metric_3
4   0  90    13.333    61.993  0.017871
3   0  89    13.333    61.998  0.017746
2   0  88    13.333    61.992  0.017711
1   0  87    13.333    61.993  0.017569
0   0  86    13.333    61.989  0.017444
9   1  90    33.333    61.736  0.030689
8   1  89    26.667    61.802  0.025995
7   1  88    20.000    61.864  0.022475
6   1  87    20.000    61.913  0.020058
5   1  86    13.333    61.964  0.018511   # <--

为两列(['id','t'])排序值,替换它们的值:

In [366]: df[['id','t']] = df[['id','t']].sort_values(['id','t'], ascending=[1,0]).values

In [367]: df
Out[367]:
   id   t  metric_1  metric_2  metric_3
0   0  90    13.333    61.989  0.017444
1   0  89    13.333    61.993  0.017569
2   0  88    13.333    61.992  0.017711
3   0  87    13.333    61.998  0.017746
4   0  86    13.333    61.993  0.017871
5   1  90    13.333    61.964  0.018511
6   1  89    20.000    61.913  0.020058
7   1  88    20.000    61.864  0.022475
8   1  87    26.667    61.802  0.025995
9   1  86    33.333    61.736  0.030689  # <--

OLD回答:

IIUC您只需按两列对数据进行排序:

In [349]: df.sort_values(['id','t'], ascending=[1,1])
Out[349]:
   id   t  metric_1  metric_2  metric_3
4   0  86    13.333    61.993  0.017871
3   0  87    13.333    61.998  0.017746
2   0  88    13.333    61.992  0.017711
1   0  89    13.333    61.993  0.017569
0   0  90    13.333    61.989  0.017444
9   1  86    33.333    61.736  0.030689
8   1  87    26.667    61.802  0.025995
7   1  88    20.000    61.864  0.022475
6   1  89    20.000    61.913  0.020058
5   1  90    13.333    61.964  0.018511

如果您想按照所需的数据集对其进行排序(替换t列值):

In [357]: df[['id','t']] = df[['id','t']].sort_values(['id','t']).values

In [358]: df
Out[358]:
   id   t  metric_1  metric_2  metric_3
0   0  86    13.333    61.989  0.017444
1   0  87    13.333    61.993  0.017569
2   0  88    13.333    61.992  0.017711
3   0  89    13.333    61.998  0.017746
4   0  90    13.333    61.993  0.017871
5   1  86    13.333    61.964  0.018511
6   1  87    20.000    61.913  0.020058
7   1  88    20.000    61.864  0.022475
8   1  89    26.667    61.802  0.025995
9   1  90    33.333    61.736  0.030689   # 1 90 33.333 61.736 0.030689 as in your desired DF

答案 1 :(得分:0)

如果你想扭转“反对”的话。列保持所有其他列不变,您可以尝试以下代码:

df.t=df['t'].sort_values(ascending=False)