Selecting all Columns based on Row value in a Dataframe Pandas
<blockquote>
  <p>I have a similar df1 with thousand columns and thousands rows. I would
  like to do a random sampling based on a condition in cells in row 1
  (date0) Basically i would like to filter the columns and return
  them and the Datetime index based on the condition if the cell on
  date0 row is equal to V1  and then do the same sampling for cell==V2 and then
  V3..etc.</p>
  
  <p>Then I would concatenate all those samples into a singular
  dataframe. I want to make sure I return the original Datetime Index
  and not a generic Index 0,1,2,3...</p>
</blockquote>

<pre><code>        abc   def   ghi   jkl   mno    pqr
date0   'V1'  'V1'  'V2'  'V3'  'V0'  'V1'

date1     2     5    6     3      2    1

date2     3     1    1     3      5    6

date3     4     4    2     7      8    0
</code></pre>

<p>To filter I have tried this so far but it does not work 
Dataset1=  <code>Dataset.ix[:,(random.sample(list(Dataset.iloc[0,:]=='V2'), 4))].copy()</code>
4 is just an arbitrary number for the number of columns to return.
Then I would need to concatenate.</p>

<p>thanks!</p>

<p>You want to include <code>date0</code> as part of the column index.</p>

<pre><code>df1 = df.T.set_index('date0', append=True).T
df1
</code></pre>

<p><a href="http://i.stack.imgur.com/WFJx3.png" rel="nofollow"><img src="http://i.stack.imgur.com/WFJx3.png" alt="enter image description here"></a></p>

<p>Then you can use <code>xs</code> to take cross sections</p>

<pre><code>df1.xs('V1', axis=1, level=1)
</code></pre>

<p><a href="http://i.stack.imgur.com/ImWI2.png" rel="nofollow"><img src="http://i.stack.imgur.com/ImWI2.png" alt="enter image description here"></a></p>

<hr>

<p><strong><em>Response to Comment</em></strong><br>
This works for using the first row without knowing the row index value</p>

<pre><code>df1 = df.iloc[1:].T.set_index(df.iloc[0], append=True).T

df1.xs('V1', axis=1, level=1)
</code></pre>

<hr>

<p><strong><em>Response to 2nd Comment</em></strong><br>
<code>iloc[1:]</code> is intended to explicity drop the first row.  If you want to keep it, don't include that part.</p>

<pre><code>df1 = df.T.set_index(df.iloc[0], append=True).T
df1
</code></pre>

<p><a href="http://i.stack.imgur.com/XDT99.png" rel="nofollow"><img src="http://i.stack.imgur.com/XDT99.png" alt="enter image description here"></a></p>

<pre><code>df1.xs('V1', axis=1, level=1)
</code></pre>

<p><a href="http://i.stack.imgur.com/iErTJ.png" rel="nofollow"><img src="http://i.stack.imgur.com/iErTJ.png" alt="enter image description here"></a></p>

python pandas random dataframe