基于时间戳分组生成值

2024-05-12 22:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

有这样一个数据帧:

enter image description here

我们有时间戳(time列)和机器的4个不同部分在type列:第1部分、第2部分、第3部分、第4部分和激活列,其中只有值1(它被激活)。我想将rest类添加到每个时间戳中,并将0放入activated列中,如下所示:

enter image description here

我假设应该使用带有列表的分组方法,但我不知道该怎么做。这在熊猫身上可能吗


Tags: 数据方法机器rest列表timetype时间
1条回答
网友
1楼 · 发布于 2024-05-12 22:41:17

通过使用unstackstack

df.set_index(['time','type']).activation.unstack(fill_value=0).stack().reset_index()

或者用pivotmelt

df.pivot(*df.columns).stack(dropna=False).fillna(0).reset_index()

相关问题 更多 >