getting scalar form dataframe
<p>how to retrieve single scalar from a pandas dataframe column using filtering on another. I am using .value[0] but I would like something better.</p>

<pre><code>df['Age_in_years'][ df['Sample_id'] == id_sample ].values[0]
df.loc[df['Sample_id'] == id_sample, 'Age_in_years'].values[0]
</code></pre>

<p>You can call <code>idxmax()</code> on the condition series, which returns:</p>

<blockquote>
  <p>Index of first occurrence of maximum of values.</p>
</blockquote>

<p>Which in this case is the <em>Index of the first True</em>, and then use <code>loc</code> to find the corresponding value: </p>

<pre><code>df = pd.DataFrame({'id': [1,2,3,4,5,6], 'value': [2,2,2,3,3,3]})

df
#  id   value
#0  1   2
#1  2   2
#2  3   2
#3  4   3
#4  5   3
#5  6   3

df.loc[(df.value == 3).idxmax(), 'id']
# 4
</code></pre>

python pandas