title: Pandas drop before first valid index and after last valid index for each column of a dataframe
body: <p>I have a dataframe like this:</p>
  
  <pre><code>df = pd.DataFrame({'timestamp':pd.date_range('2018-01-01', '2018-01-02', freq='2h', closed='right'),'col1':[np.nan, np.nan, np.nan, 1,2,3,4,5,6,7,8,np.nan], 'col2':[np.nan, np.nan, 0, 1,2,3,4,5,np.nan,np.nan,np.nan,np.nan], 'col3':[np.nan, -1, 0, 1,2,3,4,5,6,7,8,9], 'col4':[-2, -1, 0, 1,2,3,4,np.nan,np.nan,np.nan,np.nan,np.nan]
                })[['timestamp', 'col1', 'col2', 'col3', 'col4']]
  </code></pre>
  
  <p>which looks like this:</p>
  
  <pre><code>             timestamp  col1  col2  col3  col4
  0  2018-01-01 02:00:00   NaN   NaN   NaN  -2.0
  1  2018-01-01 04:00:00   NaN   NaN  -1.0  -1.0
  2  2018-01-01 06:00:00   NaN   0.0   NaN   0.0
  3  2018-01-01 08:00:00   1.0   1.0   1.0   1.0
  4  2018-01-01 10:00:00   2.0   NaN   2.0   2.0
  5  2018-01-01 12:00:00   3.0   3.0   NaN   3.0
  6  2018-01-01 14:00:00   NaN   4.0   4.0   4.0
  7  2018-01-01 16:00:00   5.0   NaN   5.0   NaN
  8  2018-01-01 18:00:00   6.0   NaN   6.0   NaN
  9  2018-01-01 20:00:00   7.0   NaN   7.0   NaN
  10 2018-01-01 22:00:00   8.0   NaN   8.0   NaN
  11 2018-01-02 00:00:00   NaN   NaN   9.0   NaN
  </code></pre>
  
  <p>Now, I want to find an efficient and pythonic way of chopping off (for each column! Not counting timestamp) before the first valid index and after the last valid index. In this example I have 4 columns, but in reality I have a lot more, 600 or so. I am looking for a way of chop of all the NaN values before the first valid index and all the NaN values after the last valid index.</p>
  
  <p>One way would be to loop through I guess.. But is there a better way? This way has to be efficient. I tried to "unpivot" the dataframe using melt, but then this didn't help. </p>
  
  <p>An obvious point is that each column would have a different number of rows after the chopping. So I would like the result to be a list of data frames (one for each column) having timestamp and the column in question. For instance:</p>
  
  <pre><code>             timestamp  col1   
  3  2018-01-01 08:00:00   1.0  
  4  2018-01-01 10:00:00   2.0   
  5  2018-01-01 12:00:00   3.0   
  6  2018-01-01 14:00:00   NaN   
  7  2018-01-01 16:00:00   5.0   
  8  2018-01-01 18:00:00   6.0   
  9  2018-01-01 20:00:00   7.0   
  10 2018-01-01 22:00:00   8.0    
  </code></pre>
  
  <p><strong>My try</strong></p>
  
  <p>I tried like this:</p>
  
  <pre><code>final = []
  columns = [c for c in df if c !='timestamp']
  for col in columns:
      first = df.loc[:, col].first_valid_index()
      last = df.loc[:, col].last_valid_index()
      final.append(df.loc[:, ['timestamp', col]].iloc[first:last+1, :])
  </code></pre>