0

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?

3
  • what would be the week number for 31? Can you have other years? Commented Aug 2, 2023 at 8:06
  • Also is there a relationship between week and timeframe? 2023-06-14 is indeed the week 24 of the year, but 2023-06-12 is not week 28 Commented Aug 2, 2023 at 8:13
  • there was a relation for the timeframe those were actually weeks of the year but while writing the question they got changed. yea I'm trying to convert the weeks of the year that is current week of the year to 0 and give numbering to previous weeks as follows. Commented Aug 3, 2023 at 9:56

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

oh this helps for me I wanted the greatest week to be 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.