Creating a new column based on Calendar function
up vote
1
down vote
favorite
I have a data frame:
Year Month Week_of_month Day_of_week
0 2018 1 2 1
1 2018 1 1 2
2 2018 1 2 2
3 2018 1 1 3
4 2018 1 2 3
5 2018 1 1 4
6 2018 1 2 4
7 2018 1 1 5
8 2018 1 2 5
9 2018 1 1 6
10 2018 1 2 6
11 2018 1 1 7
Which contains:
Day of week (1 to 7 -> Sunday until Saturday)
Week of the month (1 until 6 - One month has a maximum of 6 weeks)
I would like to get the corresponding day based on those 4 information:
So, for example, to get the 30th of december of 2018
, I did this:
I used this function:
calendar.setfirstweekday(calendar.SUNDAY)
calendar.monthcalendar(2018,12)
[[0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 31, 0, 0, 0, 0, 0]]
calendar.monthcalendar(2018,12)[5][1]
31
The point is:
How do I use this function to get each day in each row of my dataframe ?.
I tried this:
df['Day'] = calendar.monthcalendar(df.Year, df.Month)[df.Week_of_month][df.Day_of_week]
However, I got an error:
TypeError: '>=' not supported between instances of 'str' and 'int'
python pandas numpy datetime
add a comment |
up vote
1
down vote
favorite
I have a data frame:
Year Month Week_of_month Day_of_week
0 2018 1 2 1
1 2018 1 1 2
2 2018 1 2 2
3 2018 1 1 3
4 2018 1 2 3
5 2018 1 1 4
6 2018 1 2 4
7 2018 1 1 5
8 2018 1 2 5
9 2018 1 1 6
10 2018 1 2 6
11 2018 1 1 7
Which contains:
Day of week (1 to 7 -> Sunday until Saturday)
Week of the month (1 until 6 - One month has a maximum of 6 weeks)
I would like to get the corresponding day based on those 4 information:
So, for example, to get the 30th of december of 2018
, I did this:
I used this function:
calendar.setfirstweekday(calendar.SUNDAY)
calendar.monthcalendar(2018,12)
[[0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 31, 0, 0, 0, 0, 0]]
calendar.monthcalendar(2018,12)[5][1]
31
The point is:
How do I use this function to get each day in each row of my dataframe ?.
I tried this:
df['Day'] = calendar.monthcalendar(df.Year, df.Month)[df.Week_of_month][df.Day_of_week]
However, I got an error:
TypeError: '>=' not supported between instances of 'str' and 'int'
python pandas numpy datetime
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a data frame:
Year Month Week_of_month Day_of_week
0 2018 1 2 1
1 2018 1 1 2
2 2018 1 2 2
3 2018 1 1 3
4 2018 1 2 3
5 2018 1 1 4
6 2018 1 2 4
7 2018 1 1 5
8 2018 1 2 5
9 2018 1 1 6
10 2018 1 2 6
11 2018 1 1 7
Which contains:
Day of week (1 to 7 -> Sunday until Saturday)
Week of the month (1 until 6 - One month has a maximum of 6 weeks)
I would like to get the corresponding day based on those 4 information:
So, for example, to get the 30th of december of 2018
, I did this:
I used this function:
calendar.setfirstweekday(calendar.SUNDAY)
calendar.monthcalendar(2018,12)
[[0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 31, 0, 0, 0, 0, 0]]
calendar.monthcalendar(2018,12)[5][1]
31
The point is:
How do I use this function to get each day in each row of my dataframe ?.
I tried this:
df['Day'] = calendar.monthcalendar(df.Year, df.Month)[df.Week_of_month][df.Day_of_week]
However, I got an error:
TypeError: '>=' not supported between instances of 'str' and 'int'
python pandas numpy datetime
I have a data frame:
Year Month Week_of_month Day_of_week
0 2018 1 2 1
1 2018 1 1 2
2 2018 1 2 2
3 2018 1 1 3
4 2018 1 2 3
5 2018 1 1 4
6 2018 1 2 4
7 2018 1 1 5
8 2018 1 2 5
9 2018 1 1 6
10 2018 1 2 6
11 2018 1 1 7
Which contains:
Day of week (1 to 7 -> Sunday until Saturday)
Week of the month (1 until 6 - One month has a maximum of 6 weeks)
I would like to get the corresponding day based on those 4 information:
So, for example, to get the 30th of december of 2018
, I did this:
I used this function:
calendar.setfirstweekday(calendar.SUNDAY)
calendar.monthcalendar(2018,12)
[[0, 0, 0, 0, 0, 0, 1],
[2, 3, 4, 5, 6, 7, 8],
[9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22],
[23, 24, 25, 26, 27, 28, 29],
[30, 31, 0, 0, 0, 0, 0]]
calendar.monthcalendar(2018,12)[5][1]
31
The point is:
How do I use this function to get each day in each row of my dataframe ?.
I tried this:
df['Day'] = calendar.monthcalendar(df.Year, df.Month)[df.Week_of_month][df.Day_of_week]
However, I got an error:
TypeError: '>=' not supported between instances of 'str' and 'int'
python pandas numpy datetime
python pandas numpy datetime
edited Nov 11 at 5:17
Md. Mokammal Hossen Farnan
585320
585320
asked Nov 11 at 4:51
Cesar
988
988
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Here you go:
df['Day'] = df.apply(lambda x: calendar.monthcalendar(x.Year, x.Month)[x.Week_of_month-1][x.Day_of_week-1], axis=1)
The output:
Hope this helps.
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Here you go:
df['Day'] = df.apply(lambda x: calendar.monthcalendar(x.Year, x.Month)[x.Week_of_month-1][x.Day_of_week-1], axis=1)
The output:
Hope this helps.
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
add a comment |
up vote
2
down vote
accepted
Here you go:
df['Day'] = df.apply(lambda x: calendar.monthcalendar(x.Year, x.Month)[x.Week_of_month-1][x.Day_of_week-1], axis=1)
The output:
Hope this helps.
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Here you go:
df['Day'] = df.apply(lambda x: calendar.monthcalendar(x.Year, x.Month)[x.Week_of_month-1][x.Day_of_week-1], axis=1)
The output:
Hope this helps.
Here you go:
df['Day'] = df.apply(lambda x: calendar.monthcalendar(x.Year, x.Month)[x.Week_of_month-1][x.Day_of_week-1], axis=1)
The output:
Hope this helps.
edited Nov 11 at 5:33
answered Nov 11 at 5:12
Pankaj Joshi
901310
901310
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
add a comment |
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
Most welcome Sir :)
– Pankaj Joshi
Nov 11 at 6:01
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245946%2fcreating-a-new-column-based-on-calendar-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown