1

I need to check if a series already exists as a row in the dataframe. The dataframe is as follows-

 Name Age University 0 Ankit NaN BHU 1 Aishwarya 21.0 JNU 2 Shaurya 22.0 DU 

I have tried-

(df == ser).all(1).any() 

It works fine when the dataframe and series do not have null values. For example, if the series is ['Aishwarya', 21.0, 'JNU'], the output is True, which is correct. However, if the series is ['Ankit', np.nan, 'BHU'], the output is False, even if the series exists in the dataframe.

Minimum, reproducible example for my problem-

details = {'Name':['Ankit', 'Aishwarya', 'Shaurya'], 'Age':[np.nan, 21, 22], 'University':['BHU', 'JNU', 'DU']} df = pd.DataFrame(details, columns = ['Name', 'Age', 'University']) ser1 = pd.Series(['Ankit', np.nan, 'BHU'], index = ['Name', 'Age', 'University']) ser2 = pd.Series(['Aishwarya', 21.0, 'JNU'], index = ['Name', 'Age', 'University']) print((ser1 == df).all(1).any()) print((ser2 == df).all(1).any()) 

Actual Output-

False True 

Expected Output-

True True 

2 Answers 2

3

By definition np.nan == np.nan is False

Try using df.isin() instead:

df.isin([*ser1]).all(1).any() True 
Sign up to request clarification or add additional context in comments.

2 Comments

This returns False.
Hi @Shradha, changed it. I was originally testing with lists instead of series. Turning the series into a list already works out
0

Found an answer here as well.

print(ser1.astype(str).eq(df.astype(str)).all(1).any()) # True print(ser2.astype(str).eq(df.astype(str)).all(1).any()) # True 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.