【发布时间】:2019-05-16 12:36:00
【问题描述】:
我得到了一个如下所示的数据框。 我想创建一个包含总步数的新列。 我有一张如下表。 可以看到 ID 1 有 5 个步骤。
+----+--------------------------------------------------------+
| ID | Steps |
+----+--------------------------------------------------------+
| 1 | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> |
| 2 | <DIV><P>Another step</P></DIV> |
| | <DIV><P>Something</P></DIV> |
| | <DIV><P>Something</P></DIV> |
| | <DIV><P>Something</P></DIV> |
| | <DIV><P>Something</P></DIV> |
+----+--------------------------------------------------------+
我想使用“DIV”按正确的 ID 计算总步数,并用总步数创建一个新列。
+----+--------------------------------------------------------+-------------+
| ID | Steps | Total_Steps |
+----+--------------------------------------------------------+-------------+
| 1 | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> | 10 |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> | |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> | |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> | |
| | <DIV><P>Another step</P></DIV><DIV><P>A step</P></DIV> | |
| 2 | <DIV><P>Another step</P></DIV> | 5 |
| | <DIV><P>Something</P></DIV> | |
| | <DIV><P>Something</P></DIV> | |
| | <DIV><P>Something</P></DIV> | |
| | <DIV><P>Something</P></DIV> | |
| 3 | <DIV><P>Just a step</P></DIV> | 4 |
| | <DIV><P>Just a step</P></DIV> | |
| | <DIV><P>Just a step</P></DIV> | |
| | <DIV><P>Just a step</P></DIV> | |
+----+--------------------------------------------------------+-------------+
【问题讨论】:
-
Steps列上的简单 groupby 不起作用吗? -
那么您
Steps列的每个单元格中有那么长(5/4 行)的文本吗? -
有时更像是 200 步
-
可以看到 ID 1 有 5 个步骤。不,我不能。我无法猜测您的第一个示例是多行字符串中有 2 行和 Steps 还是有 10 行。此外,您说它有 5 个步骤,在下面的示例中计数为 10。您能在此处添加一些精度吗?
标签: python regex pandas dataframe count