how to set numbering to column values?

38 views Asked by At

I'm having a df like this.

    timeframe   week
0   2023-06-14  24
1   2023-06-21  25
2   2023-06-28  26
3   2023-07-05  27
4   2023-06-12  28
5   2023-06-19  29
6   2023-06-26  30

I want to get the numbering of week 30 as 0 and 29 as 1 and so on.

    timeframe   week
0   2023-06-14  6
1   2023-06-21  5
2   2023-06-28  4
3   2023-07-05  3
4   2023-06-12  2
5   2023-06-19  1
6   2023-06-26  0

Any solutions?

1

There are 1 answers

1
mozway On

One easy solution would be to subtract the week from 30:

df['week'] = df['week'].rsub(30)

# or
df['week'] = 30 - df['week']

This however wouldn't consider weeks that are potentially greater than 30 (they would become negative). If you goal is to make the greatest week number 0 and increment the others in reverse order:

df['week'] = df['week'].rsub(df['week'].max())

Output:

    timeframe  week
0  2023-06-14     6
1  2023-06-21     5
2  2023-06-28     4
3  2023-07-05     3
4  2023-06-12     2
5  2023-06-19     1
6  2023-06-26     0