Subsetting column from pandas dataframe with indexing
<p>I have a large data set in a pandas dataframe I would like to subset in order to further manipulate. </p>

<p>For example, I have a df that looks like this:</p>

<pre><code>Sample  Group   AMP         ADP         ATP
1       A       0.3840396   0.55635504  0.5844648
2       A       0.3971521   0.57851902  -0.24603208
3       A       0.4578926   0.68118957  0.19129746
4       B       0.400222    0.58370811  0.01782915
5       B       0.4110945   0.60208593  -0.6285537
6       B       0.3307011   -0.82615087 -0.25354715
7       C       0.3485679   -0.79597002 -0.17294609
8       C       0.3408411   -0.8090222  0.76138965
9       C       0.3856457   -0.73333568 0.27364299
</code></pre>

<p>Lets say I want to make a new dataframe <code>df2</code> from <code>df</code> that contains only the samples in group B and only the corresponding values for ATP. I should be able to do this from indexing alone.(?)</p>

<p>I would like to do something like this:</p>

<pre><code>df2 = df[(df['Group']=='B') &amp; (df['ATP'])]
</code></pre>

<p>I know <code>df['ATP']</code> obviously is not the correct way to do this. Printing <code>df2</code> yields this:</p>

<pre><code>   Sample Group       AMP       ADP       ATP
   4      B           0.400222  0.583708  0.017829
   5      B           0.411094  0.602086 -0.628554
   6      B           0.330701 -0.826151 -0.253547
</code></pre>

<p>Ideally, <code>df2</code> would look like this:</p>

<pre><code>   Sample Group    ATP
   4      B        0.017829
   5      B       -0.628554
   6      B       -0.253547
</code></pre>

<p>Is there a way to do this without resorting to some convoluted looping or simply manually deleting the unwanted columns and their values?</p>

<p>Thanks!!!</p>

<pre><code>df2 = df.loc[df['Group'] == 'B', ['Sample', 'Group', 'ATP']]
</code></pre>

python pandas subset