Given a numpy array such as the one below, is it possible to use vectorization to return the associated value in column HHt without using a for loop?
ex_arr = [2643, 2644, 2647]
for i in ex_arr:
h_p = df.at[i, "HHt"]
Example df:
| HHt | |
|---|---|
| 2643 | 1 |
| 2644 | 2 |
| 2645 | 3 |
| 2646 | 4 |
| 2647 | 5 |
| 2648 | 6 |
| 2649 | 7 |
| 2650 | 8 |
Expected result:
1
2
5
>Solution :
Use DataFrame.loc, if need list or array add Series.to_list or Series.to_numpy:
print (df.loc[ex_arr,'HHt'].to_list())
[1, 2, 5]
print (df.loc[ex_arr,'HHt'].to_numpy())
[1 2 5]