简体   繁体   English

如果特定列包含 null 值,如何从 dataframe 中删除行?

[英]How to delete rows from a dataframe if specific columns contains null values?

I need to delete rows from a dataframe if specific columns contains null values:如果特定列包含 null 值,我需要从 dataframe 中删除行:

-> In this example, if col2 and col3 are null: -> 在这个例子中,如果 col2 和 col3 是 null:

import pandas as pd

obj = {'col1': [1, 2,7,47,12,67,58], 'col2': [741, 332,7,'Nan',127,'Nan',548],  'col3': ['Nan', 2,74,'Nan',127,'Nan',548] }

df = pd.DataFrame(data=obj)



df.head()
    col1 col2   col3
0   1    741    Nan
1   2    332    2
2   7    7      74
3   47   Nan    Nan
4   12   127    127
5   67   Nan    Nan
6   58   548    548

After delete, the result should be:删除后,结果应该是:

df.head()
        col1 col2   col3
    0   1    741    Nan
    1   2    332    2
    2   7    7      74
    4   12   127    127
    6   58   548    548

Thanks for all!谢谢大家!

Use Boolean indexing with DataFrame.isna or DataFrame.isnull to check NaN or Null values.使用Boolean indexingDataFrame.isnaDataFrame.isnull值来检查 NaN 或 ZBBB93CDD216E3C18014Z10B1 Select the maximum number of NaN allowed per rows with DataFrame.sum and Series.le : Select DataFrame.sumSeries.le每行允许的最大NaN数:

df=df.replace('Nan',np.nan)
new_df=df[df.isnull().sum(axis=1).le(1)]
print(new_df)

   col1   col2   col3
0     1  741.0    NaN
1     2  332.0    2.0
2     7    7.0   74.0
4    12  127.0  127.0
6    58  548.0  548.0

To specifict columns:要指定列:

DataFrame.all DataFrame.all

df=df.replace('Nan',np.nan)
df_filtered=df[~df[['col2','col3']].isnull().all(axis=1)]
print(df_filtered)

   col1   col2   col3
0     1  741.0    NaN
1     2  332.0    2.0
2     7    7.0   74.0
4    12  127.0  127.0
6    58  548.0  548.0

Using dropna使用dropna

axis = 0 to delete rows, thresh=1 has the number of non-null values required to drop the row. axis = 0删除行, thresh=1具有删除行所需的非空值的数量。

You can use subset=['col2', 'col3'] if you want to define the columns on which the as the basis of dropping rows.如果要定义作为删除行基础的列,可以使用subset=['col2', 'col3']

You can try this:你可以试试这个:

df = df.dropna(axis=0, subset=['col2', 'col3'], how="any", thresh=1)

After deploying the solution proposed by @ansev, everything worked:部署@ansev 提出的解决方案后,一切正常:

import pandas as pd

obj = {'col1': [1, 2,7,47,12,67,58], 'col2': [741, 332,7,'Nan',127,'Nan',548],  'col3': ['Nan', 2,74,'Nan',127,'Nan',548] }

df = pd.DataFrame(data=obj)

df=df.replace('Nan',np.nan)
df_filtered=df[~df[['col2','col3']].isnull().all(axis=1)]

print(df_filtered)

col1   col2   col3
0     1  741.0    NaN
1     2  332.0    2.0
2     7    7.0   74.0
4    12  127.0  127.0
6    58  548.0  548.0

暂无
暂无

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

相关问题 如何根据特定列中的空值从数据框中删除行? - How to drop rows from a dataframe as per null values in a specific column? 从 dataframe 中删除特定行 - delete specific rows from a dataframe Python:使用来自特定行的计算值将列添加到数据框 - Python: adding columns to dataframe using calculated values from specific rows 使用来自另一个 dataframe 的具有特定列值的行创建一个新的数据框 - create a new dataframes with rows from another dataframe with specific columns values 根据值从特定范围列中删除Pandas DataFrame中的行 - Deleting rows in Pandas DataFrame based on values, from a specific range columns 如何根据数据框列的某些特定值删除行? - How to delete rows based on some specific values of a dataframe column? 如果列值列表为 NULL,如何从 Pandas 数据框中删除行? - How to delete rows from pandas data frame if list of columns values are NULL? 从具有特定单词的列中删除包含 null 值的行 [Python] - drop the rows that contain the null values from columns with a specific word [Python] Pandas 如何根据所有行的值、应用于整个数据帧的特定列值向数据帧添加新列 - Pandas how add a new column to dataframe based on values from all rows, specific columns values applied to whole dataframe 如果任何特定列包含特定值,则删除 pandas 数据框中的行 - Remove rows in pandas dataframe if any of specific columns contains a specific value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM