因此,如果您按时间戳排序,并在定义唯一行的列上使用groupby,您可以获得所需的所有信息。使用last 获取每组的最后一行,使用nth 获取倒数第二行:
>>> groups = df.sort_values('timestamp').groupby(['A', 'B'])
>>> groups.last()
Population Start End timestamp
A B
A1 B1 100 2021-05-15 00:00:00 2021-06-30 00:00:00 2021-07-06 00:00:00
A2 B3 350 2021-05-10 00:00:00 2021-05-12 00:00:00 2021-07-06 00:00:00
B4 125 2021-06-02 00:00:00 2021-06-04 00:00:00 2021-07-06 00:00:00
>>> groups.nth(-2)
A1 B1 250 2021-05-30 00:00:00 2021-06-02 00:00:00 2021-06-06 00:00:00
现在所有这些数据帧都在列 A 和 B 上建立索引,因此您可以简单地在 join 添加后缀,重置索引,然后就完成了:
>>> mod = groups.last().join(groups.nth(-2), rsuffix='_prev').reset_index()
>>> mod
A B Population Start End timestamp Population_prev Start_prev End_prev timestamp_prev
0 A1 B1 100 2021-05-15 00:00:00 2021-06-30 00:00:00 2021-07-06 00:00:00 250.0 2021-05-30 00:00:00 2021-06-02 00:00:00 2021-06-06 00:00:00
1 A2 B3 350 2021-05-10 00:00:00 2021-05-12 00:00:00 2021-07-06 00:00:00 NaN NaN NaN NaN
2 A2 B4 125 2021-06-02 00:00:00 2021-06-04 00:00:00 2021-07-06 00:00:00 NaN NaN NaN NaN
然后是一些细节,让它看起来像你所拥有的:
>>> col_order = [
... *df.columns[:2],
... *(new_col for col in df.columns[2:-1] for new_col in [col, f'{col}_prev']),
... 'type', 'timestamp'
... ]
>>> row_type = mod['timestamp_prev'].isna().map({True: 'New', False: 'Mod'})
>>> mod.join(row_type.rename('type')).reindex(col_order, axis='columns')
A B Population Population_prev Start Start_prev End End_prev type timestamp
0 A1 B1 100 250.0 2021-05-15 00:00:00 2021-05-30 00:00:00 2021-06-30 00:00:00 2021-06-02 00:00:00 Mod 2021-07-06 00:00:00
1 A2 B3 350 NaN 2021-05-10 00:00:00 NaN 2021-05-12 00:00:00 NaN New 2021-07-06 00:00:00
2 A2 B4 125 NaN 2021-06-02 00:00:00 NaN 2021-06-04 00:00:00 NaN New 2021-07-06 00:00:00
另一种适用于任意数量重复值的技术是使用pivot。让我们使用相同的 groupby 但使用 cumcount() 来定义列的顺序:
>>> num = df.sort_values('timestamp').groupby(['A', 'B']).cumcount().rename('num')
>>> num
1 0
0 1
2 0
3 0
Name: num, dtype: int64
>>> pvt = df.join(num).pivot(index=['A', 'B'], columns='num', values=['Population', 'Start', 'End'])
>>> pvt
Population Start End
num 0 1 0 1 0 1
A B
A1 B1 250 100 2021-05-30 00:00:00 2021-05-15 00:00:00 2021-06-02 00:00:00 2021-06-30 00:00:00
A2 B3 350 NaN 2021-05-10 00:00:00 NaN 2021-05-12 00:00:00 NaN
B4 125 NaN 2021-06-02 00:00:00 NaN 2021-06-04 00:00:00 NaN
如您所见,它可以满足您的需求,但在列中具有多索引。让我们将其展平为普通列,我们就完成了:
>>> pvt.columns = [f'{col}_prev{n if n > 1 else ""}' if n > 0 else col for col, n in pvt.columns]
>>> pvt.reset_index()
A B Population Population_prev Start Start_prev End End_prev
0 A1 B1 250 100 2021-05-30 00:00:00 2021-05-15 00:00:00 2021-06-02 00:00:00 2021-06-30 00:00:00
1 A2 B3 350 NaN 2021-05-10 00:00:00 NaN 2021-05-12 00:00:00 NaN
2 A2 B4 125 NaN 2021-06-02 00:00:00 NaN 2021-06-04 00:00:00 NaN