1

I'm currently working with pandas DataFrames. While iterating over it I want to check if a value is numpy.nan or a list.

for i, row in df.iterrows(): value = row["Name"] if pd.isnull(value): dosomething() 

This works just fine, except if

type(value) == list 

Then I thought of maybe putting any() around:

for i, row in df.iterrows(): value = row["Name"] if any(pd.isnull(value)): dosomething() 

But now I get a exception everytime a NaN is in value because it's obviously not iterable.

Is there a better solution then checking the type of value?

1 Answer 1

4

Use or:

for i, row in df.iterrows(): value = row["Name"] if pd.isnull(value) or (type(value) == list): dosomething() 

Another way for check is isinstance:

for i, row in df.iterrows(): value = row["Name"] if pd.isnull(value) or isinstance(value, list): dosomething() 
Sign up to request clarification or add additional context in comments.

3 Comments

But I still get an errormessage if type(value)==list (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())
@aAnnAa - Maybe duplicated columns, then need any(pd.isnull(value) or (type(value) == list))
It works now with "if not isinstance(value, list) and pd.isnull(value)", thanks a lot for your help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.