title: How to calculate the average of the most recent three non-nan value using Python
body: <p>I have a dataframe <code>df</code> looks like the following. I want to calculate the average of the last 3 non nan columns. If there are less than three non-missing columns then the average number is missing.</p>
  
  <pre><code>name day1 day2 day3 day4  day5 day6 day7
  A    1     1   nan   2    3    0   3
  B    nan   nan nan   nan  nan  nan 3
  C    1     1   0     1    1    1   1
  D    1     1   0     1    nan  1   4
  </code></pre>
  
  <p>The expect output should looks like the following </p>
  
  <pre><code>name day1 day2 day3 day4  day5 day6 day7    expected 
  A    1     1   nan   2    3    0   3        2     &lt;-  1/3*(day5 + day6 + day7)
  B    nan   nan nan   nan  nan  nan 3        nan   &lt;-  less than 3 non-missing
  C    1     1   0     1    1    1   1        1     &lt;-  1/3*(day5 + day6 + day7)
  D    1     1   0     1    nan  1   4        2    &lt;-  1/3 *(day4 + day6 + day7)
  </code></pre>
  
  <p>I know how to calculate the average of the last three column and count how many non-missing observation are there. 
  <code>df.iloc[:, 5:7].count(axis=1) average of the last three column</code>
  <code>df.iloc[:, 5:7].count(axis=1) number of non-nan in the last three column</code></p>
  
  <p>If there are less than 3 non-missing observation, I know how to set the average value to missing using  <code>df.iloc[:, 1:7].count(axis=1) &lt;= 3</code>. </p>
  
  <p>But I am struggling to find a way to calculate the average of the last three non-missing columns. Can anyone teach me how to solve this please?</p>