简体   繁体   中英

assign a number id for every 4 rows in pandas dataframe

I have a pandas dataframe like this:

pd.DataFrame({'week': ['2019-w01', '2019-w02','2019-w03','2019-w04',
                         '2019-w05','2019-w06','2019-w07','2019-w08',
                         '2019-w9','2019-w10','2019-w11','2019-w12'],
               'value': [11,22,33,34,57,88,2,9,10,1,76,14],
               'period': [1,1,1,1,2,2,2,2,3,3,3,3]})


    week    value
0   2019-w1 11
1   2019-w2 22
2   2019-w3 33
3   2019-w4 34
4   2019-w5 57
5   2019-w6 88
6   2019-w7 2
7   2019-w8 9
8   2019-w9 10
9   2019-w10    1
10  2019-w11    76
11  2019-w12    14

what I need is like below. I would like to assign a period ID every 4-week interval.

    week      value period
0   2019-w01    11  1
1   2019-w02    22  1
2   2019-w03    33  1
3   2019-w04    34  1
4   2019-w05    57  2
5   2019-w06    88  2
6   2019-w07    2   2
7   2019-w08    9   2
8   2019-w9    10   3
9   2019-w10    1   3
10  2019-w11    76  3
11  2019-w12    14  3

what is the best way to achieve that? Thanks.

try with:

df['period']=(pd.to_numeric(df['week'].str.split('-').str[-1]
                 .str.replace('w',''))//4).shift(fill_value=0).add(1)
print(df)

        week  value  period
0   2019-w01     11       1
1   2019-w02     22       1
2   2019-w03     33       1
3   2019-w04     34       1
4   2019-w05     57       2
5   2019-w06     88       2
6   2019-w07      2       2
7   2019-w08      9       2
8    2019-w9     10       3
9   2019-w10      1       3
10  2019-w11     76       3
11  2019-w12     14       3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM