0

I have a list:

list = ['firstname', 'lastname', 'email', 'phonenumber']

I want to iterate for this list as:

import pandas as pd

df = pd.read_csv(filepath)

df[ pd.notnull(df[firstname]) | pd.notnull(df[lastname]) | 
    pd.notnull(df[email]) | pd.notnull(df[phonenumber])]

How do I perform the above process using a loop?

2 Answers 2

1

You can filter using columns with null values.

df.isnull().any()
>> 
firstname True
lastname True
...

df.isnull().sum()

It shows all columns and the total NaNs of each column (your list).

Sign up to request clarification or add additional context in comments.

Comments

0

First, do not shadow built-in class names:

L = ['firstname', 'lastname', 'email', 'phonenumber']

Then use notnull with any along axis=1 to construct a Boolean series mask:

res = df[df[L].notnull().any(1)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.