2024-05-12 22:41:17 发布
网友
有这样一个数据帧:
我们有时间戳(time列)和机器的4个不同部分在type列:第1部分、第2部分、第3部分、第4部分和激活列,其中只有值1(它被激活)。我想将rest类添加到每个时间戳中,并将0放入activated列中,如下所示:
time
type
1
activated
我假设应该使用带有列表的分组方法,但我不知道该怎么做。这在熊猫身上可能吗
通过使用unstack和stack
unstack
stack
df.set_index(['time','type']).activation.unstack(fill_value=0).stack().reset_index()
或者用pivot和melt
pivot
melt
df.pivot(*df.columns).stack(dropna=False).fillna(0).reset_index()
通过使用
unstack
和stack
或者用
pivot
和melt
相关问题 更多 >
编程相关推荐