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

Slicing python dataframe with isin

I am trying something I think is simple, however I keep getting errors and I don’t know why.

I am trying to set a value in a new column for df2. If value is column from df2 matches any value from df1 "col", then write "result", otherwise "no result".

#Create a series from df column 
series_from_df = df1['Col']
df2['new_col'] = 'result' if df2['Col1'].isin(series_from_df) else 'Not result'

The above gets me an error:

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

(<class ‘ValueError’>, ValueError(‘The truth value of a Series is
ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().’),
<traceback object at 0x7f9081a28f80>)

Then I try the below adding the square brakes for series_from_df

#Create a series from df column 
series_from_df = df1['Col']
df2['new_col'] = 'result' if df2['Col1'].isin([series_from_df]) else 'Not result'

I get the same error than before.

What am I missing?

>Solution :

df2['Col1'].isin(df1['Col1']) is a boolean Series, but you’re trying to use it as a condition in if, which is expecting a truth-value. You can use numpy.where instead where the Series created by the isin is used as the condition:

df2['new_col'] = np.where(df2['Col1'].isin(df1['Col1']), 'result', 'Not result')
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