如何检查 Pandas DataFrame 中的任何值是否为 NaN
How to check if any value is NaN in a Pandas DataFrame
在 Python Pandas 中,检查 DataFrame 是否具有一个(或多个)NaN 值的最佳方法是什么?
我知道函数 pd.isnan
,但是这个 returns 每个元素的布尔值 DataFrame。 这里也没有完全回答我的问题。
df.isnull().any().any()
应该做到。
你有几个选择。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,6))
# Make a few areas have NaN values
df.iloc[1:3,1] = np.nan
df.iloc[5,3] = np.nan
df.iloc[7:9,5] = np.nan
现在数据框看起来像这样:
0 1 2 3 4 5
0 0.520113 0.884000 1.260966 -0.236597 0.312972 -0.196281
1 -0.837552 NaN 0.143017 0.862355 0.346550 0.842952
2 -0.452595 NaN -0.420790 0.456215 1.203459 0.527425
3 0.317503 -0.917042 1.780938 -1.584102 0.432745 0.389797
4 -0.722852 1.704820 -0.113821 -1.466458 0.083002 0.011722
5 -0.622851 -0.251935 -1.498837 NaN 1.098323 0.273814
6 0.329585 0.075312 -0.690209 -3.807924 0.489317 -0.841368
7 -1.123433 -1.187496 1.868894 -2.046456 -0.949718 NaN
8 1.133880 -0.110447 0.050385 -1.158387 0.188222 NaN
9 -0.513741 1.196259 0.704537 0.982395 -0.585040 -1.693810
- 选项 1:
df.isnull().any().any()
- 这 return 是一个布尔值
你知道 isnull()
会 return 像这样的数据框:
0 1 2 3 4 5
0 False False False False False False
1 False True False False False False
2 False True False False False False
3 False False False False False False
4 False False False False False False
5 False False False True False False
6 False False False False False False
7 False False False False False True
8 False False False False False True
9 False False False False False False
如果你做到了 df.isnull().any()
,你可以只找到具有 NaN
个值的列:
0 False
1 True
2 False
3 True
4 False
5 True
dtype: bool
还有一个.any()
会告诉你以上是否有True
> df.isnull().any().any()
True
- 选项 2:
df.isnull().sum().sum()
- 这 return 是 NaN
值总数的整数:
这与 .any().any()
的操作方式相同,首先给出列中 NaN
个值的总和,然后是这些值的总和:
df.isnull().sum()
0 0
1 2
2 0
3 1
4 0
5 2
dtype: int64
最后,获取DataFrame中NaN值的总数:
df.isnull().sum().sum()
5
jwilner 的回复很到位。我正在探索是否有更快的选择,因为根据我的经验,对平面数组求和(奇怪地)比计数快。这段代码似乎更快:
df.isnull().values.any()
import numpy as np
import pandas as pd
import perfplot
def setup(n):
df = pd.DataFrame(np.random.randn(n))
df[df > 0.9] = np.nan
return df
def isnull_any(df):
return df.isnull().any()
def isnull_values_sum(df):
return df.isnull().values.sum() > 0
def isnull_sum(df):
return df.isnull().sum() > 0
def isnull_values_any(df):
return df.isnull().values.any()
perfplot.save(
"out.png",
setup=setup,
kernels=[isnull_any, isnull_values_sum, isnull_sum, isnull_values_any],
n_range=[2 ** k for k in range(25)],
)
df.isnull().sum().sum()
有点慢,但当然还有其他信息 -- NaNs
.
的数量
根据您处理的数据类型,您还可以在执行 EDA 时通过将 dropna 设置为 False 来获取每列的值计数。
for col in df:
print df[col].value_counts(dropna=False)
适用于分类变量,当您有很多唯一值时效果不佳。
如果您需要知道有多少行带有“一个或多个NaN
s”:
df.isnull().T.any().T.sum()
或者,如果您需要提取这些行并进行检查:
nan_rows = df[df.isnull().T.any()]
因为 pandas
必须为 DataFrame.dropna()
找到这个,我看了一下他们是如何实现的,发现他们使用了 DataFrame.count()
,这算作所有非- DataFrame
中的空值。比照。 pandas source code。我没有对这项技术进行基准测试,但我认为图书馆的作者可能已经做出了明智的选择。
既然none提到了,就多了一个变量hasnans
。
如果 pandas 系列中的一个或多个值是 NaN,df[i].hasnans
将输出到 True
,否则输出 False
。请注意,它不是函数。
pandas 版本“0.19.2”和“0.20.2”
加入 Hobs 的精彩回答,我对 Python 和 Pandas 很陌生,所以如果我错了请指出。
找出哪些行有 NaN:
nan_rows = df[df.isnull().any(1)]
将执行相同的操作,而无需通过将 any() 的轴指定为 1 来检查行中是否存在 'True' 来进行转置。
刚用
math.isnan(x)、Return 如果 x 是 NaN(不是数字)则为真,否则为假。
找出特定列中哪些行有 NaN:
nan_rows = df[df['name column'].isnull()]
或者您可以在 DF
上使用 .info()
例如:
df.info(null_counts=True)
其中 returns 列中的 non_null 行数,例如:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3276314 entries, 0 to 3276313
Data columns (total 10 columns):
n_matches 3276314 non-null int64
avg_pic_distance 3276314 non-null float64
这是另一种查找 null 并替换为计算值的有趣方法
#Creating the DataFrame
testdf = pd.DataFrame({'Tenure':[1,2,3,4,5],'Monthly':[10,20,30,40,50],'Yearly':[10,40,np.nan,np.nan,250]})
>>> testdf2
Monthly Tenure Yearly
0 10 1 10.0
1 20 2 40.0
2 30 3 NaN
3 40 4 NaN
4 50 5 250.0
#Identifying the rows with empty columns
nan_rows = testdf2[testdf2['Yearly'].isnull()]
>>> nan_rows
Monthly Tenure Yearly
2 30 3 NaN
3 40 4 NaN
#Getting the rows# into a list
>>> index = list(nan_rows.index)
>>> index
[2, 3]
# Replacing null values with calculated value
>>> for i in index:
testdf2['Yearly'][i] = testdf2['Monthly'][i] * testdf2['Tenure'][i]
>>> testdf2
Monthly Tenure Yearly
0 10 1 10.0
1 20 2 40.0
2 30 3 90.0
3 40 4 160.0
4 50 5 250.0
超级简单的语法:df.isna().any(axis=None)
Starting from v0.23.2, you can use DataFrame.isna
+ DataFrame.any(axis=None)
其中 axis=None
指定整个 DataFrame 的逻辑缩减。
# Setup
df = pd.DataFrame({'A': [1, 2, np.nan], 'B' : [np.nan, 4, 5]})
df
A B
0 1.0 NaN
1 2.0 4.0
2 NaN 5.0
df.isna()
A B
0 False True
1 False False
2 True False
df.isna().any(axis=None)
# True
有用的替代品
numpy.isnan
如果您 运行 是 pandas.
的旧版本,则另一个高性能选项
np.isnan(df.values)
array([[False, True],
[False, False],
[ True, False]])
np.isnan(df.values).any()
# True
或者,检查总和:
np.isnan(df.values).sum()
# 2
np.isnan(df.values).sum() > 0
# True
Series.hasnans
也可以迭代调用Series.hasnans
。例如,要检查单个列是否有 NaN,
df['A'].hasnans
# True
并且要检查 任何 列是否有 NaN,您可以使用带有 any
的理解(这是一个短路操作)。
any(df[c].hasnans for c in df)
# True
这实际上非常快。
df.apply(axis=0, func=lambda x : any(pd.isnull(x)))
将检查每列是否包含 Nan。
我一直在使用以下内容并将其类型转换为字符串并检查 nan 值
(str(df.at[index, 'column']) == 'nan')
这让我可以检查系列中的特定值,而不仅仅是 return 如果它包含在系列中的某处。
最好使用:
df.isna().any().any()
这里是why。所以用isna()
来定义isnull()
,当然这两个是一样的
这比接受的答案还要快,并且涵盖了所有二维熊猫数组。
df.isnull().sum()
这将为您提供 DataFrame 的各个列中存在的所有 NaN 值的计数。
让 df
成为 Pandas DataFrame 的名称,任何 numpy.nan
的值都是空值。
如果您想查看哪些列有空值,哪些列没有(只是 True 和 False)
df.isnull().any()
如果您只想查看包含空值的列
df.loc[:, df.isnull().any()].columns
如果您想查看每列中的空值数
df.isna().sum()
如果您想查看每一列中的空值百分比
df.isna().sum()/(len(df))*100
如果您想查看仅包含空值的列中空值的百分比:
df.loc[:,list(df.loc[:,df.isnull().any()].columns)].isnull().sum()/(len(df))*100
编辑 1:
如果您想直观地查看数据丢失的地方:
import missingno
missingdata_df = df.columns[df.isnull().any()].tolist()
missingno.matrix(df[missingdata_df])
import missingno as msno
msno.matrix(df) # just to visualize. no missing value.
您不仅可以检查是否存在任何 'NaN',还可以使用以下方法获取每列中 'NaN' 的百分比,
df = pd.DataFrame({'col1':[1,2,3,4,5],'col2':[6,np.nan,8,9,10]})
df
col1 col2
0 1 6.0
1 2 NaN
2 3 8.0
3 4 9.0
4 5 10.0
df.isnull().sum()/len(df)
col1 0.0
col2 0.2
dtype: float64
我们可以通过使用 seaborn 模块生成热图来查看数据集中存在的空值heatmap
import pandas as pd
import seaborn as sns
dataset=pd.read_csv('train.csv')
sns.heatmap(dataset.isnull(),cbar=False)
为此,我们可以使用语句 df.isna().any()
。这将检查我们所有的列和 return True
是否有任何缺失值或 NaN
s,或 False
如果没有缺失值。
试试下面的方法
df.isnull().sum()
或
df.isna().values.any()
另一种方法是 dropna
并检查长度是否相等:
>>> len(df.dropna()) != len(df)
True
>>>
我建议使用 values 属性,因为对数组的计算要快得多。
arr = np.random.randn(100, 100)
arr[40, 40] = np.nan
df = pd.DataFrame(arr)
%timeit np.isnan(df.values).any() # 7.56 µs
%timeit np.isnan(df).any() # 627 µs
%timeit df.isna().any(axis=None) # 572 µs
结果:
7.56 µs ± 447 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
627 µs ± 40.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
572 µs ± 15.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
注意:您需要 运行 %timeit
在 Jupyter notebook 中工作
Bar representation for missing values
import missingno
missingno.bar(df)# will give you exact no of values and values missing
在 Python Pandas 中,检查 DataFrame 是否具有一个(或多个)NaN 值的最佳方法是什么?
我知道函数 pd.isnan
,但是这个 returns 每个元素的布尔值 DataFrame。
df.isnull().any().any()
应该做到。
你有几个选择。
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,6))
# Make a few areas have NaN values
df.iloc[1:3,1] = np.nan
df.iloc[5,3] = np.nan
df.iloc[7:9,5] = np.nan
现在数据框看起来像这样:
0 1 2 3 4 5
0 0.520113 0.884000 1.260966 -0.236597 0.312972 -0.196281
1 -0.837552 NaN 0.143017 0.862355 0.346550 0.842952
2 -0.452595 NaN -0.420790 0.456215 1.203459 0.527425
3 0.317503 -0.917042 1.780938 -1.584102 0.432745 0.389797
4 -0.722852 1.704820 -0.113821 -1.466458 0.083002 0.011722
5 -0.622851 -0.251935 -1.498837 NaN 1.098323 0.273814
6 0.329585 0.075312 -0.690209 -3.807924 0.489317 -0.841368
7 -1.123433 -1.187496 1.868894 -2.046456 -0.949718 NaN
8 1.133880 -0.110447 0.050385 -1.158387 0.188222 NaN
9 -0.513741 1.196259 0.704537 0.982395 -0.585040 -1.693810
- 选项 1:
df.isnull().any().any()
- 这 return 是一个布尔值
你知道 isnull()
会 return 像这样的数据框:
0 1 2 3 4 5
0 False False False False False False
1 False True False False False False
2 False True False False False False
3 False False False False False False
4 False False False False False False
5 False False False True False False
6 False False False False False False
7 False False False False False True
8 False False False False False True
9 False False False False False False
如果你做到了 df.isnull().any()
,你可以只找到具有 NaN
个值的列:
0 False
1 True
2 False
3 True
4 False
5 True
dtype: bool
还有一个.any()
会告诉你以上是否有True
> df.isnull().any().any()
True
- 选项 2:
df.isnull().sum().sum()
- 这 return 是NaN
值总数的整数:
这与 .any().any()
的操作方式相同,首先给出列中 NaN
个值的总和,然后是这些值的总和:
df.isnull().sum()
0 0
1 2
2 0
3 1
4 0
5 2
dtype: int64
最后,获取DataFrame中NaN值的总数:
df.isnull().sum().sum()
5
jwilner 的回复很到位。我正在探索是否有更快的选择,因为根据我的经验,对平面数组求和(奇怪地)比计数快。这段代码似乎更快:
df.isnull().values.any()
import numpy as np
import pandas as pd
import perfplot
def setup(n):
df = pd.DataFrame(np.random.randn(n))
df[df > 0.9] = np.nan
return df
def isnull_any(df):
return df.isnull().any()
def isnull_values_sum(df):
return df.isnull().values.sum() > 0
def isnull_sum(df):
return df.isnull().sum() > 0
def isnull_values_any(df):
return df.isnull().values.any()
perfplot.save(
"out.png",
setup=setup,
kernels=[isnull_any, isnull_values_sum, isnull_sum, isnull_values_any],
n_range=[2 ** k for k in range(25)],
)
df.isnull().sum().sum()
有点慢,但当然还有其他信息 -- NaNs
.
根据您处理的数据类型,您还可以在执行 EDA 时通过将 dropna 设置为 False 来获取每列的值计数。
for col in df:
print df[col].value_counts(dropna=False)
适用于分类变量,当您有很多唯一值时效果不佳。
如果您需要知道有多少行带有“一个或多个NaN
s”:
df.isnull().T.any().T.sum()
或者,如果您需要提取这些行并进行检查:
nan_rows = df[df.isnull().T.any()]
因为 pandas
必须为 DataFrame.dropna()
找到这个,我看了一下他们是如何实现的,发现他们使用了 DataFrame.count()
,这算作所有非- DataFrame
中的空值。比照。 pandas source code。我没有对这项技术进行基准测试,但我认为图书馆的作者可能已经做出了明智的选择。
既然none提到了,就多了一个变量hasnans
。
df[i].hasnans
将输出到 True
,否则输出 False
。请注意,它不是函数。
pandas 版本“0.19.2”和“0.20.2”
加入 Hobs 的精彩回答,我对 Python 和 Pandas 很陌生,所以如果我错了请指出。
找出哪些行有 NaN:
nan_rows = df[df.isnull().any(1)]
将执行相同的操作,而无需通过将 any() 的轴指定为 1 来检查行中是否存在 'True' 来进行转置。
刚用 math.isnan(x)、Return 如果 x 是 NaN(不是数字)则为真,否则为假。
找出特定列中哪些行有 NaN:
nan_rows = df[df['name column'].isnull()]
或者您可以在 DF
上使用 .info()
例如:
df.info(null_counts=True)
其中 returns 列中的 non_null 行数,例如:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3276314 entries, 0 to 3276313
Data columns (total 10 columns):
n_matches 3276314 non-null int64
avg_pic_distance 3276314 non-null float64
这是另一种查找 null 并替换为计算值的有趣方法
#Creating the DataFrame
testdf = pd.DataFrame({'Tenure':[1,2,3,4,5],'Monthly':[10,20,30,40,50],'Yearly':[10,40,np.nan,np.nan,250]})
>>> testdf2
Monthly Tenure Yearly
0 10 1 10.0
1 20 2 40.0
2 30 3 NaN
3 40 4 NaN
4 50 5 250.0
#Identifying the rows with empty columns
nan_rows = testdf2[testdf2['Yearly'].isnull()]
>>> nan_rows
Monthly Tenure Yearly
2 30 3 NaN
3 40 4 NaN
#Getting the rows# into a list
>>> index = list(nan_rows.index)
>>> index
[2, 3]
# Replacing null values with calculated value
>>> for i in index:
testdf2['Yearly'][i] = testdf2['Monthly'][i] * testdf2['Tenure'][i]
>>> testdf2
Monthly Tenure Yearly
0 10 1 10.0
1 20 2 40.0
2 30 3 90.0
3 40 4 160.0
4 50 5 250.0
超级简单的语法:df.isna().any(axis=None)
Starting from v0.23.2, you can use DataFrame.isna
+ DataFrame.any(axis=None)
其中 axis=None
指定整个 DataFrame 的逻辑缩减。
# Setup
df = pd.DataFrame({'A': [1, 2, np.nan], 'B' : [np.nan, 4, 5]})
df
A B
0 1.0 NaN
1 2.0 4.0
2 NaN 5.0
df.isna()
A B
0 False True
1 False False
2 True False
df.isna().any(axis=None)
# True
有用的替代品
numpy.isnan
如果您 运行 是 pandas.
np.isnan(df.values)
array([[False, True],
[False, False],
[ True, False]])
np.isnan(df.values).any()
# True
或者,检查总和:
np.isnan(df.values).sum()
# 2
np.isnan(df.values).sum() > 0
# True
Series.hasnans
也可以迭代调用Series.hasnans
。例如,要检查单个列是否有 NaN,
df['A'].hasnans
# True
并且要检查 任何 列是否有 NaN,您可以使用带有 any
的理解(这是一个短路操作)。
any(df[c].hasnans for c in df)
# True
这实际上非常快。
df.apply(axis=0, func=lambda x : any(pd.isnull(x)))
将检查每列是否包含 Nan。
我一直在使用以下内容并将其类型转换为字符串并检查 nan 值
(str(df.at[index, 'column']) == 'nan')
这让我可以检查系列中的特定值,而不仅仅是 return 如果它包含在系列中的某处。
最好使用:
df.isna().any().any()
这里是why。所以用isna()
来定义isnull()
,当然这两个是一样的
这比接受的答案还要快,并且涵盖了所有二维熊猫数组。
df.isnull().sum()
这将为您提供 DataFrame 的各个列中存在的所有 NaN 值的计数。
让 df
成为 Pandas DataFrame 的名称,任何 numpy.nan
的值都是空值。
如果您想查看哪些列有空值,哪些列没有(只是 True 和 False)
df.isnull().any()
如果您只想查看包含空值的列
df.loc[:, df.isnull().any()].columns
如果您想查看每列中的空值数
df.isna().sum()
如果您想查看每一列中的空值百分比
df.isna().sum()/(len(df))*100
如果您想查看仅包含空值的列中空值的百分比:
df.loc[:,list(df.loc[:,df.isnull().any()].columns)].isnull().sum()/(len(df))*100
编辑 1:
如果您想直观地查看数据丢失的地方:
import missingno
missingdata_df = df.columns[df.isnull().any()].tolist()
missingno.matrix(df[missingdata_df])
import missingno as msno
msno.matrix(df) # just to visualize. no missing value.
您不仅可以检查是否存在任何 'NaN',还可以使用以下方法获取每列中 'NaN' 的百分比,
df = pd.DataFrame({'col1':[1,2,3,4,5],'col2':[6,np.nan,8,9,10]})
df
col1 col2
0 1 6.0
1 2 NaN
2 3 8.0
3 4 9.0
4 5 10.0
df.isnull().sum()/len(df)
col1 0.0
col2 0.2
dtype: float64
我们可以通过使用 seaborn 模块生成热图来查看数据集中存在的空值heatmap
import pandas as pd
import seaborn as sns
dataset=pd.read_csv('train.csv')
sns.heatmap(dataset.isnull(),cbar=False)
为此,我们可以使用语句 df.isna().any()
。这将检查我们所有的列和 return True
是否有任何缺失值或 NaN
s,或 False
如果没有缺失值。
试试下面的方法
df.isnull().sum()
或
df.isna().values.any()
另一种方法是 dropna
并检查长度是否相等:
>>> len(df.dropna()) != len(df)
True
>>>
我建议使用 values 属性,因为对数组的计算要快得多。
arr = np.random.randn(100, 100)
arr[40, 40] = np.nan
df = pd.DataFrame(arr)
%timeit np.isnan(df.values).any() # 7.56 µs
%timeit np.isnan(df).any() # 627 µs
%timeit df.isna().any(axis=None) # 572 µs
结果:
7.56 µs ± 447 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
627 µs ± 40.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
572 µs ± 15.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
注意:您需要 运行 %timeit
在 Jupyter notebook 中工作
Bar representation for missing values
import missingno
missingno.bar(df)# will give you exact no of values and values missing