简体   繁体   English

Pandas:过滤在几个特定列中的任何一个中具有 Null/None/NaN 值的行

[英]Pandas: Filter in rows that have a Null/None/NaN value in any of several specific columns

I have a csv file which has a lot of strings called "NULL" in it, in several columns.我有一个 csv 文件,其中有很多名为"NULL"的字符串,在几列中。

I would like to select (filter in) rows that have a "NULL" value in any of several specific columns.我想选择(过滤)在几个特定列中的任何一个中具有"NULL"值的行。

Example:例子:

 ["Firstname"] ["Lastname"] ["Profession"] "Jeff" "Goldblum" "NULL" "NULL" "Coltrane" "Musician" "Richard" "NULL" "Physicist"

Here, I would like to filter in (select) rows in df that have the value "NULL" in the column "Firstname" or "Lastname" – but not if the value is "NULL" in "Profession" .在这里,我想过滤(选择) df中在"Firstname""Lastname"列中具有值"NULL"的行 - 但如果"Profession"的值为"NULL" ,则不过滤。

This manages to filter in strings (not None ) in one column:这设法在一列中过滤字符串(不是None ):

df = df[df["Firstname"].str.contains("NULL", case=False)]


I have however attempted to convert the "NULL" strings to None via:然而,我试图通过以下方式将"NULL"字符串转换为None

df = df.where((pd.notnull(df)), None)
df.columns = df.columns.str.lower()

Given the above str.contains filtering, perhaps it's easier to filter in "NULL" strings before converting to None ?鉴于上述str.contains过滤,也许在转换为None之前过滤"NULL"字符串更容易?

I think you need first replace NULL string to NaN .我认为您首先需要replace NULL字符串replaceNaN Then check all NaN values in selected columns by isnull and select all rows where is any True by boolean indexing :然后通过isnull检查所选列中的所有NaN值,并通过boolean indexing选择any True的所有行:

df = df.replace("NULL", np.nan)

print (df[['Firstname','Lastname']].isnull())
  Firstname Lastname
0     False    False
1      True    False
2     False     True

print (df[df[['Firstname','Lastname']].isnull().any(1)])
  Firstname  Lastname Profession
1       NaN  Coltrane   Musician
2   Richard       NaN  Physicist

you can try:你可以试试:

df.replace(to_replace="NULL", value = None)

to replace all the occurence of "NULL" to None将所有出现的"NULL"替换为None

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

相关问题 Pandas 删除几列不是nan的行 - Pandas remove rows where several columns are not nan 如果任何特定列包含特定值,则删除 pandas 数据框中的行 - Remove rows in pandas dataframe if any of specific columns contains a specific value 用必须在另一列的特定行上计算的公式填充值为0或NaN的Pandas数据框行 - Fill Pandas dataframe rows, whose value is a 0 or NaN, with a formula that have to be calculated on specific rows of another column 当特定列在 pandas 中具有 null 值时,Select 数据和计数 - Select data and count when specific columns have null value in pandas 当特定列在pandas中具有空值时选择数据 - Select data when specific columns have null value in pandas 如何为特定列值过滤pandas DataFrame并仅返回不包含NAN的列? - How to filter a pandas DataFrame for a certain column value and only return columns that do not have NAN? 选择列中具有特定值的行,并在 Pandas 数据框中包含具有 NaN 的行 - Select rows with specific values in columns and include rows with NaN in pandas dataframe Pandas 根据一定数量的某些列为 NaN 过滤行 - Pandas filter rows based on certain number of certain columns being NaN Python Pandas 过滤列名称 有一个或多个 NaN - Python Pandas Filter names of Columns What have one or more NaN Pandas 过滤到特定行,然后合并 2 列 - Pandas filter to specific rows and then combine 2 columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM