pandas Python连接连续行

pkln4tw6  于 2023-01-28  发布在  Python
关注(0)|答案(1)|浏览(168)

我的数据示例:

name  day        text    
john  1 october  hello
john  1 october  world
mary  1 october  good friend
john  1 october  python is cool
peter 1 october  love is
peter 1 october  in the air

当列“name”和“day”在连续行中相同时,我想连接“text”。“day”不总是“1 October”,并且数据集非常大。这就是我想要的:

name  day        text
john  1 october  hello world
mary  1 october  good friend
john  1 october  python is cool
peter 1 october  love is in the air

下面是我的代码:

data = data.fillna(' ')
data = data.groupby(['name', 'day'], as_index=False).agg({'text': ' '.join})

我正在获得但不是我想要的东西:

name  day        text
john  1 october  hello world python is cool
mary  1 october  good friend
peter 1 october  love is in the air

我应该如何更改我的代码?

8nuwlpux

8nuwlpux1#

shiftanycumsum制作一个定制的石斑鱼:

group = df[['name', 'day']].ne(df[['name', 'day']].shift()).any(axis=1).cumsum()

df.groupby(['name', 'day', group], as_index=False).agg({'text': ' '.join})

输出:

name        day                text
0   john  1 october         hello world
1   john  1 october      python is cool
2   mary  1 october         good friend
3  peter  1 october  love is in the air

相关问题