python - 计算数据帧中两个连续行之间的集合差异

标签 python pandas set

我需要一些技巧来进行计算。

我的数据框如下所示:

text_id     name     date                words
1           John     2018-01-01          {ocean, blue}
1           John     2018-02-01          {ocean, green} 
2           Anne     2018-03-01          {table, chair}
3           Anne     2018-03-01          {hot, cold, warm}
3           Mark     2018-04-01          {hot, cold}
3           Ethan    2018-05-01          {warm, icy}
4           Paul     2018-01-01          {cat, dog, puppy}
4           John     2018-02-01          {cat}
5           Paul     2018-03-01          {cat, sheep, deer}

在文本中,text_id代表特定文本(SAME TEXT_ID = SAME TEXT)。 name 列代表编辑文本的人。 date 列代表用户进行编辑的日期。 words 列由用户编辑后形成文本的单词组成。

words 列是一个集合。我需要添加一个附加列 added_words,其中包含先前对 THE SAME 文本进行编辑的设置差异。这是为了检查一次编辑与其在同一文本中的连续编辑之间有什么区别。

此处的示例输出为:

text_id     name     date          words            added_words
1           John     2018-01-01    {ocean,blue}     {ocean, blue}
1           John     2018-02-01    {ocean,green}    {green}
2           Anne     2018-03-01    {table,chair}    {table, chair}
3           Anne     2018-03-01    {hot,cold,warm}  {hot, cold, warm}
3           Mark     2018-04-01    {hot,cold}       {}
3           Ethan    2018-05-01    {warm,icy}       {warm, icy}
4           Paul     2018-01-01    {cat,dog,puppy}  {cat, dog, puppy}
4           John     2018-02-01    {cat}            {}
5           Paul     2018-03-01    {cat,sheep,deer} {cat,sheep,deer}

请注意,基本上,added_words 列包含第 i 行中的单词列与第 i-1 行中的单词列之间的集合差异,仅当第 i 行和第 i-1 行中的 text_id 相同,因为:我只想要相同文本之间的差异(相同的 text_id),而不是不同的文本.

任何有关这方面的提示都会非常有帮助。

编辑:

为了将 words 列变成一个集合,请执行以下操作:

df['words'] = df['words'].str.strip('{}').str.split(',').apply(set)

最佳答案

使用difffillnaDiff 将执行集合减法

df['added_words'] = df.groupby('text_id').words.diff().fillna(df.words)

In [162]: df
Out[162]:
   text_id   name        date               words         added_words
0        1   John  2018-01-01       {ocean, blue}       {ocean, blue}
1        1   John  2018-02-01      {green, ocean}             {green}
2        2   Anne  2018-03-01      {chair, table}      {chair, table}
3        3   Anne  2018-03-01   {warm, cold, hot}   {warm, cold, hot}
4        3   Mark  2018-04-01         {cold, hot}                  {}
5        3  Ethan  2018-05-01         {warm, icy}         {warm, icy}
6        4   Paul  2018-01-01   {cat, puppy, dog}   {cat, puppy, dog}
7        4   John  2018-02-01               {cat}                  {}
8        5   Paul  2018-03-01  {cat, deer, sheep}  {cat, deer, sheep}

关于python - 计算数据帧中两个连续行之间的集合差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57495578/

相关文章:

python - 如何绘制 Pandas 数据框的特定列?

python - 复制行的期望索引列

java - EnumSet.spliterator 没有特征 Spliterator.NONNULL

python - 这个字符串是如何在 python 中格式化的

python - 将 map() 函数(来自 Pool 类)返回的列表转换为 Python 中的字典

python - 递归 DFS 最短路径实现未按预期工作

python - 如何根据给定值获取列的百分比

python - 在进行合并时 reshape Pandas 数据框

python - python 集合理解如何工作?

algorithm - 如何有效地确定两个列表是否包含以相同方式排序的元素?