有条件地合并 Pandas DataFrame 中的行

Flu*_*uxy 1 python pandas

我有以下熊猫数据帧:

col1 col2                   col3        col4 
A    2021-03-28 01:40:00    1.381158    0.0
A    2021-03-28 01:50:00    0.480089    0.0
A    2021-03-28 03:00:00    0.000000    0.0
A    2021-03-28 03:00:00    0.111088    0.0
A    2021-03-28 03:10:00    0.000000    0.0
A    2021-03-28 03:10:00    0.000000    0.0
A    2021-03-28 03:10:00    0.151066    0.0
B    2021-03-28 03:10:00    1.231341    1.0
Run Code Online (Sandbox Code Playgroud)

我需要合并具有相同col1col2值的行,并为col3.

这是预期的输出:

col1 col2                   col3        col4 
A    2021-03-28 01:40:00    1.381158    0.0
A    2021-03-28 01:50:00    0.480089    0.0
A    2021-03-28 03:00:00    0.111088    0.0
A    2021-03-28 03:10:00    0.151066    0.0
B    2021-03-28 03:10:00    1.231341    1.0
Run Code Online (Sandbox Code Playgroud)

我怎样才能进行这种合并?

Erf*_*fan 5

我们可以使用groupby+idxmax来做到这一点:

idx = df.groupby(["col1", "col2"])["col3"].idxmax().to_numpy()
df.loc[idx]
Run Code Online (Sandbox Code Playgroud)
  col1                 col2  col3  col4
0    A  2021-03-28 01:40:00  1.38  0.00
1    A  2021-03-28 01:50:00  0.48  0.00
3    A  2021-03-28 03:00:00  0.11  0.00
6    A  2021-03-28 03:10:00  0.15  0.00
7    B  2021-03-28 03:10:00  1.23  1.00
Run Code Online (Sandbox Code Playgroud)