我有一个 Dataframe ,如下所示:
text
0 dog went to the store.
0 cat is tall.
0 blue is red.
1 red is blue
1 blue is red
如何按行连接字符串,按索引连接组,使新的df看起来像这样?
text
0 dog went to the store. cat is tall. blue is red.
1 red is blue.blue is red
我试过了,但这什么也没做,返回的行数也是一样的,不知道该怎么办:
df[['text']].groupby(df.index)['text'].transform(lambda x: ','.join(x))
1条答案
按热度按时间7gcisfzg1#
可能的解决方案(只需将
transform
替换为agg
):输出量: