简体   繁体   English

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

[英]How to filter data in python using pandas?

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? 如何使用循环执行上述过程?

You can filter using columns with null values. 您可以使用具有空值的columns进行filter

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

df.isnull().sum()

It shows all columns and the total NaNs of each column (your list). 它显示所有列以及每列的总NaN(您的列表)。

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: 然后使用沿axis=1 any notnull来构造布尔系列掩码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM