Creating a new column based on Calendar function









up vote
1
down vote

favorite
1












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:



Image



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'









share|improve this question



























    up vote
    1
    down vote

    favorite
    1












    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:



    Image



    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'









    share|improve this question

























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      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:



      Image



      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'









      share|improve this question















      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:



      Image



      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 5:17









      Md. Mokammal Hossen Farnan

      585320




      585320










      asked Nov 11 at 4:51









      Cesar

      988




      988






















          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:



          enter image description here



          Hope this helps.






          share|improve this answer






















          • Most welcome Sir :)
            – Pankaj Joshi
            Nov 11 at 6:01










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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:



          enter image description here



          Hope this helps.






          share|improve this answer






















          • Most welcome Sir :)
            – Pankaj Joshi
            Nov 11 at 6:01














          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:



          enter image description here



          Hope this helps.






          share|improve this answer






















          • Most welcome Sir :)
            – Pankaj Joshi
            Nov 11 at 6:01












          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:



          enter image description here



          Hope this helps.






          share|improve this answer














          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:



          enter image description here



          Hope this helps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3