Python - Access column based on another column value
<p>I have the following dataframe in python</p>

<pre><code>+-------+--------+
| Value | Number |
+-------+--------+
| true  |    123 |
| false |    234 |
| true  |    345 |
| true  |    456 |
| false |    567 |
| false |    678 |
| false |    789 |
+-------+--------+
</code></pre>

<p>How do I conduct an operation which returns a list of all the 'Number' which has Value == TRUE</p>

<p>The output list expected from the above table is</p>

<pre><code>['123', '345', '456']
</code></pre>

<p>Thanks in advance!</p>

<p><code>df.loc[df['Value'],'Number']</code> should work assuming the dtype for 'Value' are real booleans:</p>

<pre><code>In [68]:
df.loc[df['Value'],'Number']

Out[68]:
0    123
2    345
3    456
Name: Number, dtype: int64
</code></pre>

<p>The above uses boolean indexing, here the boolean values are a mask against the df.</p>

<p>If you want a <code>list</code>:</p>

<pre><code>In [69]:
df.loc[df['Value'],'Number'].tolist()

Out[69]:
[123, 345, 456]
</code></pre>

python pandas