title: How to get last day of each month from a data frame and remove the rest from the dataframe?
body: <p><a href="https://i.stack.imgur.com/3l8Qq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3l8Qq.jpg" alt="enter image description here" /></a></p>
  <p>Hi all,
  The image is the data frame I am working on to learn python.</p>
  <p>From this dataframe, I am trying to find the row records which are the last day of Dec for each year. My objective is to keep the record highlighted in yellow in the data frame and remove the white rows.</p>
  <p>For example, for the year 2010, I just want to keep the 3rd record and remove rows 1 to 2.
  As for the year 2011, I want to remove rows 4 to 7 and keep row 8.</p>
  <p>Below is the code I have written. I intend to use loop to find the records I want to keep and remove the rest.</p>
  <p>To retain records using month values, I managed to meet my objective by keeping Dec and remove Jan to Nov records</p>
  <p>However, for days (last 3 lines of code), I realized that the last day does not always end with 31 in the data frame and I cannot use my initial logic to remove.</p>
  <p>May I seek help if is there a better solution to find the last day of the month in data frame and remove the rest?</p>
  <p>Thanks</p>
  <pre><code>amalgamate=pd.read_excel(&quot;amalgamate.xlsx&quot;)
  
  ##Create last 3 columns to segregate Year, Month and Day.
  amalgamate[&quot;Date&quot;] = pd.to_datetime(amalgamate[&quot;Date&quot;], errors = &quot;raise&quot;, format = &quot;%Y-%m-%d&quot;)
  amalgamate[&quot;Year&quot;]=amalgamate[&quot;Date&quot;].dt.year
  amalgamate[&quot;Month&quot;]=amalgamate[&quot;Date&quot;].dt.month
  amalgamate[&quot;Day&quot;]=amalgamate[&quot;Date&quot;].dt.day
  
  
  listofMonth=amalgamate.Month.unique()
  listofDay=amalgamate.Day.unique()
  
  #Loop through the records and remove records that are not Dec for each year
  for eachmonth in listofMonth:
      if eachmonth !=12:
          amalgamate=amalgamate[amalgamate.Month != eachmonth]
          
  #Loop through the records and remove records that are not 31 for each month
  for eachday in listofDay:
      if eachday !=31:
          amalgamate=amalgamate[amalgamate.Day != eachday]
  </code></pre>