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 print whether each string in a list is in a pandas dataframe?

Given a list of search terms and a pandas dataframe, what is the most pythonic way to print whether the search term is present in the target dataframe?

search_terms = ["red", "blue", "green", "orange"]

input_df looks like…

    color  count
0     red     15
1    blue     39
2  yellow     40
3   green     21

I want to see…

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

red = true
blue = true
green = true
orange = false

I know how to filter the input_df to include only search_terms. This doesn’t alert me to the fact that "orange" was not located in the input_df. The search_terms could contain hundreds or thousands of strings.

color = ['red', 'blue', 'yellow', 'green']
count = [15,39,40,21]

input_dict = dict(color=color, count=count)
input_df = pd.DataFrame(data=input_dict)

found_df = input_df[input_df['color'].isin(search_terms)]

>Solution :

You can try:

out = dict(zip(search_terms, pd.Series(search_terms).isin(input_df['color'])))

Or:

out = dict(zip(search_terms, np.isin(search_terms, input_df)) )

Output:

{'red': True, 'blue': True, 'green': True, 'orange': False}
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