Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if two columns match some value

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading