【问题标题】:How to count some rows within a record and make a new column with the total count?如何计算记录中的某些行并用总计数创建一个新列?
【发布时间】: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


【解决方案1】:

Series.str.countGroupBy.transformsum 一起使用:

df['Total_Steps'] = df['Steps'].str.count('<DIV>').groupby(df['ID'].ffill()).transform('sum')
print (df)
   ID                                              Steps  Total_Steps
0   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...           10
1   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...           10
2   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...           10
3   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...           10
4   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...           10
5   2                     <DIV><P>Another step</P></DIV>            5
6   2                        <DIV><P>Something</P></DIV>            5
7   2                        <DIV><P>Something</P></DIV>            5
8   2                        <DIV><P>Something</P></DIV>            5
9   2                        <DIV><P>Something</P></DIV>            5

如果只需要第一个值,请添加 numpy.whereSeries.duplicated

s = df['Steps'].str.count('<DIV>').groupby(df['ID'].ffill()).transform('sum')
df['Total_Steps'] = np.where(df['ID'].duplicated(), np.nan, s)
#possible mixed values - numeric with empty strings, but then some function should failed
#df['Total_Steps'] = np.where(df['ID'].duplicated(), '', s)
print (df)
   ID                                              Steps  Total_Steps
0   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...         10.0
1   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...          NaN
2   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...          NaN
3   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...          NaN
4   1  <DIV><P>Another step</P></DIV><DIV><P>A step</...          NaN
5   2                     <DIV><P>Another step</P></DIV>          5.0
6   2                        <DIV><P>Something</P></DIV>          NaN
7   2                        <DIV><P>Something</P></DIV>          NaN
8   2                        <DIV><P>Something</P></DIV>          NaN
9   2                        <DIV><P>Something</P></DIV>          NaN

【讨论】:

    【解决方案2】:

    为什么不这样:

    df['Total_Steps']=df['steps'].str.contains('\<Div\>\<P\>').count()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多