python - Pandas 根据条件组合连续的行

标签 python pandas

我的问题与这个类似,但答案似乎并不完全有效!!

merge rows pandas dataframe based on condition

给定以下 pandas 数据框:

+---------+-----------------+-----------------+
| SECTION | TEXT            | NUMBER_OF_WORDS |
+---------+-----------------+-----------------+
| ONE     | lots   of text… | 55              |
+---------+-----------------+-----------------+
| ONE     | word1           | 1               |
+---------+-----------------+-----------------+
| ONE     | lots   of text… | 151             |
+---------+-----------------+-----------------+
| ONE     | word2           | 1               |
+---------+-----------------+-----------------+
| ONE     | word3           | 1               |
+---------+-----------------+-----------------+
| ONE     | word4           | 1               |
+---------+-----------------+-----------------+
| TWO     | lots   of text… | 523             |
+---------+-----------------+-----------------+
| TWO     | lots   of text… | 123             |
+---------+-----------------+-----------------+
| TWO     | word4           | 1               |
+---------+-----------------+-----------------+

如果 NUMBER_OF_WORDS 列中的值为 1;它必须与上面的行结合;前提是它们具有相同的 SECTION 值。

因此最终的结果应该是这样的:

+---------+--------------------------------------+-----------------+
| SECTION | TEXT                                 | NUMBER_OF_WORDS |
+---------+--------------------------------------+-----------------+
| ONE     | lots   of text…, word1               | 56              |
+---------+--------------------------------------+-----------------+
| ONE     | lots   of text…, word2, word3, word4 | 154             |
+---------+--------------------------------------+-----------------+
| TWO     | lots   of text…                      | 523             |
+---------+--------------------------------------+-----------------+
| TWO     | lots   of text…, word4               | 124             |
+---------+--------------------------------------+-----------------+

这是代码;这似乎有效,但不是我想要的。

df.groupby(['SECTION', (df.NUMBER_OF_WORDS.shift(1) == 1)], as_index=False, sort=False).agg({'TEXT': lambda x: ', '.join(x), 'NUMBER_OF_WORDS': lambda x: sum(x)})

更新

这是BEN_YO的回答;但他似乎有一个小错字。为了让 future 的用户回答这个问题,我将稍微修改一下他的答案。

s = df['NUMBER_OF_WORDS'].ne(1).cumsum()
out = df.groupby(s).agg({'SECTION': 'first','TEXT': lambda x: ', '.join(x),'NUMBER_OF_WORDS': lambda x: sum(x)})

最佳答案

让我们用 cumsum 试试 groupby

s = df['NUMBER_OF_WORDS'].ne(1).cumsum()
out = df.groupby(s).agg({'SECTION':'first','TEXT':','.join,'NUMBER_OF_WORDS':'sum'})

关于python - Pandas 根据条件组合连续的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64508280/

相关文章:

python - 通过python代码附加JSON文件

python - 是否可以使用 lambda 作为字典默认值?

python - DataFrame iterrows() 和 .to_csv : Writing row by row

python - Pandas 数据框到 excel 文件中的特定工作表而不会丢失格式

python - 应该如何管理 '.hg/' 状态目录中的 hook 特定文件?

Python/Seaborn : What does the inside horizontal distribution of the data-points means or is it random?

python - 如何使用 pandas 系列对 if 条件进行矢量化?

python - 在 Pandas 数据框中按递增顺序重新排序节点

python - 基于其他条目的 Pandas 对列的操作

python - 如何在 python/pandas 中解压缩/解聚合分层数据?