我有一个列表:
list = ['firstname', 'lastname', 'email', 'phonenumber']
我要将此列表迭代为:
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])]
如何使用循环执行上述过程?
答案 0 :(得分:1)
您可以filter
与空值一起使用columns
。
df.isnull().any()
>>
firstname True
lastname True
...
df.isnull().sum()
它显示所有列以及每列的总NaN(您的列表)。
答案 1 :(得分:0)
首先,不要隐藏内置的类名:
L = ['firstname', 'lastname', 'email', 'phonenumber']
然后将notnull
与any
和axis=1
一起使用,以构建布尔序列掩码:
res = df[df[L].notnull().any(1)]