我有一个输入数据框,其内容如下:
NAME TEXT
Tim Tim Wagner is a teacher.
Tim He is from Cleveland, Ohio.
Frank Frank is a musician.
Tim He like to travel with his family
Frank He is a performing artist who plays the cello.
Frank He performed at the Carnegie Hall last year.
Frank It was fantastic listening to him.
如果NAME列的连续行具有相同的值,我希望将TEXT列连接在一起。
输出数据框:
NAME TEXT
Tim Tim Wagner is a teacher. He is from Cleveland, Ohio.
Frank Frank is a musician
Tim He like to travel with his family
Frank He is a performing artist who plays the cello. He performed at the Carnegie Hall last year. It was fantastic listening to him.
使用pandas的shift函数,是最好的实现方式吗?感谢任何帮助。
谢谢
df = df.groupby([0, grp], sort=False).agg(lambda x: ','.join(x.dropna().astype(str))).reset_index().drop('group', axis=1)
- LearnAWK