I have a dataframe with two columns `A`

and `B`

. I want to take the scalar of column `B`

based on the value of column `A`

. I used `loc`

and `.value`

`[0]`

My data volume is relatively small, the main problem is to see whether the syntax of the code is correct. `.value`

seems to be deprecated.

import pandas as pd import numpy as np df = pd.DataFrame() df[['A', 'B']] = pd.DataFrame(np.arange(10).reshape((5, 2))) df1 = df.loc[df['A'] == 4, 'B'].values[0] print(df) print() print(df1)

The result is

5

Can this code be optimized?

df1 = df.loc[df['A'] == 4, 'B'].values[0]

## Answer

If you need optimalized with return some value if condition failed use `next`

with `iter`

:

a = next(iter(df.loc[df['A'] == 4, 'B']), 'no match') print (a) 5 a = next(iter(df.loc[df['A'] == 1000, 'B']), 'no match') print (a) no match

If values always matching is possible use `Series.to_numpy`

, but this failed if no match, so better not use:

df.loc[df['A'] == 4, 'B'].to_numpy()[0] #but this failed #df.loc[df['A'] == 1000, 'B'].to_numpy()[0]