user1946989 user1946989 - 2 months ago 23
Python Question

getting scalar form dataframe

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.

df['Age_in_years'][ df['Sample_id'] == id_sample ].values[0]
df.loc[df['Sample_id'] == id_sample, 'Age_in_years'].values[0]

Answer

You can call idxmax() on the condition series, which returns:

Index of first occurrence of maximum of values.

Which in this case is the Index of the first True, and then use loc to find the corresponding value:

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