首页 > 解决方案 > 根据前一行将数据添加到新列

问题描述

df
     type  content
1    task   buy xbox
2    task   buy fruit from supermarket
3    note   orange with squash\buy if cheap
4    note   apple
5    task   buy sunglassess

注释指的是其正上方的任务。我如何操纵 df 来获得以下 df?预期输出:

         task                       comment1             comment2
1     buy xbox
2     buy fruit from supermarket   orange with squash     apple
                                   buy if cheap
3     buy sunglassess
...

标签: pythonpandasdataframe

解决方案


使用 helper通过将值与累积和进行比较Series来获取组,通过and获取计数器并重塑:taskGroupBy.cumcountDataFrame.set_indexSeries.unstack

s = df['type'].eq('task').cumsum()
g = df.groupby(s).cumcount()

df1 = (df.set_index([s, g])['content']
         .unstack(fill_value='')
         .add_prefix('comment')
         .rename(columns={'comment0':'task'})
         .reset_index(drop=True))
         
print (df1)
                         task                        comment1 comment2
0                    buy xbox                                         
1  buy fruit from supermarket  orange with squasuy if cheap    apple
2             buy sunglassess                                         

推荐阅读