Как посчитать несколько строк в записи и создать новый столбец с общим количеством? - PullRequest
0 голосов
/ 16 мая 2019

У меня есть датафрейм, как показано ниже.Я хочу сделать новый столбец с общим количеством шагов.Я получил таблицу, как показано ниже.Вы можете видеть, что 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 |                         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>                          |             |
+----+--------------------------------------------------------+-------------+

Ответы [ 2 ]

1 голос
/ 16 мая 2019

Используйте Series.str.count с GroupBy.transform и sum:

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.where с Series.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
0 голосов
/ 16 мая 2019

Почему бы не так:

df['Total_Steps']=df['steps'].str.contains('\<Div\>\<P\>').count()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...