如何使用pandas在python中过滤数据?

时间:2018-07-04 21:19:44

标签: python python-3.x pandas dataframe

我有一个列表:

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])]

如何使用循环执行上述过程?

2 个答案:

答案 0 :(得分:1)

您可以filter与空值一起使用columns

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

df.isnull().sum()

它显示所有列以及每列的总NaN(您的列表)。

答案 1 :(得分:0)

首先,不要隐藏内置的类名:

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

然后将notnullanyaxis=1一起使用,以构建布尔序列掩码:

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