Incremental assignment in pandas dataframe to determine month from week number without date element

Ah Sheng

I'm having week numbers in the dataframe from 1 to 52 e.g. [1,2,3,4,5,6,7,8,..52]

I'm trying to create a new column for month but it would mean an incremental assignment like [1,2,3,4] = 1, [5,6,7,8] = 2, .. [49,50,51,52] = 12

I tried getting the records by multiple of 4 using df[df["week"]%4==0] and then ffill it but seems like we can only assign it all to the same number which is not what I want. Instead I want to assign [1..12] accordingly. Is there another way to do this?

jezrael

Subtract 1 first and then use integer division by 4:

df = pd.DataFrame({'week':range(1,53)})

df['new'] = (df["week"] - 1)//4 
print (df.head(10))
   week  new
0     1    0
1     2    0
2     3    0
3     4    0
4     5    1
5     6    1
6     7    1
7     8    1
8     9    2
9    10    2

print (df.tail(10))
    week  new
42    43   10
43    44   10
44    45   11
45    46   11
46    47   11
47    48   11
48    49   12
49    50   12
50    51   12
51    52   12

If want starting by 1 it is possible, but last value is 13:

df['new'] = ((df["week"] - 1)//4) + 1
print (df.head(10))
   week  new
0     1    1
1     2    1
2     3    1
3     4    1
4     5    2
5     6    2
6     7    2
7     8    2
8     9    3
9    10    3

print (df.tail(10))
    week  new
42    43   11
43    44   11
44    45   12
45    46   12
46    47   12
47    48   12
48    49   13
49    50   13
50    51   13
51    52   13

If want values between 1 and 12 (but some groups has more like 4 values) use, solution by @Aryerez, thank you:

df['new'] = ((df["week"] - 1) // (52 / 12)).astype(int) + 1

print (df.head(10))
   week  new
0     1    1
1     2    1
2     3    1
3     4    1
4     5    1
5     6    2
6     7    2
7     8    2
8     9    2
9    10    3

print (df.tail(10))
    week  new
42    43   10
43    44   10
44    45   11
45    46   11
46    47   11
47    48   11
48    49   12
49    50   12
50    51   12
51    52   12

EDIT: For 5 values in each 3rd group use:

df['new'] = ((df["week"] + 4) // (52 / 12)).astype(int)

print (df.head(15))
    week  new
0      1    1
1      2    1
2      3    1
3      4    1
4      5    2
5      6    2
6      7    2
7      8    2
8      9    3
9     10    3
10    11    3
11    12    3
12    13    3
13    14    4
14    15    4

print (df.tail(15))
    week  new
37    38    9
38    39    9
39    40   10
40    41   10
41    42   10
42    43   10
43    44   11
44    45   11
45    46   11
46    47   11
47    48   12
48    49   12
49    50   12
50    51   12
51    52   12

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQLite: Get week number from date

分類Dev

Group date by week at one month

分類Dev

How to get date from week number and day in sql?

分類Dev

Is there a way in python dataframe to populate customise week number from a range of dates?

分類Dev

How to retrieve the month from a date column values in scala dataframe?

分類Dev

Find month to date and month to go on a Pyspark dataframe

分類Dev

Grab rows with max date from pandas dataframe

分類Dev

vectorize conditional assignment in pandas dataframe

分類Dev

Determine Prime Number in Python DataFrame

分類Dev

Is a date in same week, month, year of another date in swift

分類Dev

Pandas count number of trades by month

分類Dev

Get Incremental Number from two loops

分類Dev

Determine days of week from bit mask

分類Dev

How to determine if a dataframe is Pandas or Spark?

分類Dev

How to determine if a dataframe is Pandas or Spark?

分類Dev

C# - Check if a given date is the Fifth week day of a month?

分類Dev

How to find the week number per current month in PostgreSQL?

分類Dev

Vuetify remove the body title month and year element from date picker component

分類Dev

Reshaping a Dataframe in pandas(With Date)

分類Dev

Compute 3 months rolling median from pandas DataFrame with date index

分類Dev

Pandas convert string to end of month date

分類Dev

How to get month name from date in Presto

分類Dev

Add a month of padding from minimum date

分類Dev

select month and year from Date of birth IN sql

分類Dev

Changing Date Type of Pandas Dataframe

分類Dev

How to Get Only Day Name or Number in a Week in JavaScript Date()

分類Dev

Need to find the week number in a calendar quarter of a given date with Javascript

分類Dev

Efficient way to find week ending date without a calendar table

分類Dev

Determine season from Date using lubridate in R

Related 関連記事

  1. 1

    SQLite: Get week number from date

  2. 2

    Group date by week at one month

  3. 3

    How to get date from week number and day in sql?

  4. 4

    Is there a way in python dataframe to populate customise week number from a range of dates?

  5. 5

    How to retrieve the month from a date column values in scala dataframe?

  6. 6

    Find month to date and month to go on a Pyspark dataframe

  7. 7

    Grab rows with max date from pandas dataframe

  8. 8

    vectorize conditional assignment in pandas dataframe

  9. 9

    Determine Prime Number in Python DataFrame

  10. 10

    Is a date in same week, month, year of another date in swift

  11. 11

    Pandas count number of trades by month

  12. 12

    Get Incremental Number from two loops

  13. 13

    Determine days of week from bit mask

  14. 14

    How to determine if a dataframe is Pandas or Spark?

  15. 15

    How to determine if a dataframe is Pandas or Spark?

  16. 16

    C# - Check if a given date is the Fifth week day of a month?

  17. 17

    How to find the week number per current month in PostgreSQL?

  18. 18

    Vuetify remove the body title month and year element from date picker component

  19. 19

    Reshaping a Dataframe in pandas(With Date)

  20. 20

    Compute 3 months rolling median from pandas DataFrame with date index

  21. 21

    Pandas convert string to end of month date

  22. 22

    How to get month name from date in Presto

  23. 23

    Add a month of padding from minimum date

  24. 24

    select month and year from Date of birth IN sql

  25. 25

    Changing Date Type of Pandas Dataframe

  26. 26

    How to Get Only Day Name or Number in a Week in JavaScript Date()

  27. 27

    Need to find the week number in a calendar quarter of a given date with Javascript

  28. 28

    Efficient way to find week ending date without a calendar table

  29. 29

    Determine season from Date using lubridate in R

ホットタグ

アーカイブ