I have a dataframe:
d = {'hour': [1, 1, 2, 1, 2], 'value': ['alpha', 'beta', 'alpha', 'beta', 'gamma']}
df = pd.DataFrame(data=d)
and a dictionary:
di = {1: 'alpha', 2: 'gamma'}
How can I return a vector of True/False of rows where dictionary key (hour) matches its value in a dataframe’s column value.
The result should be:
np.array([True, False, False, False, True])
>Solution :
Are you looking for map:
df['hour'].map(di) == df['value']
output:
0 True
1 False
2 False
3 False
4 True
dtype: bool
It’s trivial to turn that series into numpy array if you really want.