Python 熊猫:检查熊猫数据框列中的条目是否为空
假设您有一个数据帧,其中的数字类型为float。您将如何检查是否有空的条目 示例df:Python 熊猫:检查熊猫数据框列中的条目是否为空,python,pandas,Python,Pandas,假设您有一个数据帧,其中的数字类型为float。您将如何检查是否有空的条目 示例df: df = pd.DataFrame([(.21, ), (.01, .67), (.66, .03), (.21, .18)], columns=['dogs', 'cats']) 不,不能有NAN。将isna与any df.isna().any() Out[103]: dogs False cats True dtype: bool 或者从info,您
df = pd.DataFrame([(.21, ), (.01, .67), (.66, .03), (.21, .18)],
columns=['dogs', 'cats'])
不,不能有NAN。将
isna
与any
df.isna().any()
Out[103]:
dogs False
cats True
dtype: bool
或者从info
,您可以知道非空值不等于数据帧长度
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
dogs 4 non-null float64
cats 3 non-null float64
dtypes: float64(2)
memory usage: 192.0 bytes
df.info()
范围索引:4个条目,0到3
数据列(共2列):
狗4非空浮点数64
cats 3非空浮点64
数据类型:float64(2)
内存使用:192.0字节
将isna
与任何
df.isna().any()
Out[103]:
dogs False
cats True
dtype: bool
或者从info
,您可以知道非空值不等于数据帧长度
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
dogs 4 non-null float64
cats 3 non-null float64
dtypes: float64(2)
memory usage: 192.0 bytes
df.info()
范围索引:4个条目,0到3
数据列(共2列):
狗4非空浮点数64
cats 3非空浮点64
数据类型:float64(2)
内存使用:192.0字节
您可以使用此选项将NaN替换为您想要的任何内容:
df.fillna(0, inplace=True)
df
dogs cats
0 0.21 0.00
1 0.01 0.67
2 0.66 0.03
3 0.21 0.18
您可以使用此选项将NaN替换为您想要的任何内容:
df.fillna(0, inplace=True)
df
dogs cats
0 0.21 0.00
1 0.01 0.67
2 0.66 0.03
3 0.21 0.18
当您运行该命令时,空白本身将变为NAN。所以我不确定你的输出是什么?你想让NaN消失?您可以这样做:-
df = pd.DataFrame([(.21, ), (.01, .67), (.66, .03), (.21, .18)],
columns=['dogs', 'cats']).fillna(" ")
输出:-
当运行该命令时,空白本身将变为NAN。所以我不确定你的输出是什么?你想让NaN消失?您可以这样做:-
df = pd.DataFrame([(.21, ), (.01, .67), (.66, .03), (.21, .18)],
columns=['dogs', 'cats']).fillna(" ")
输出:-
[索引的索引,df.iterrows()中的行(如果有)(np.isnan(行))]
通过迭代所有行,可以显示带有空的行的索引。[p>[索引索引,df.iterrows()中的行,如果有(np.isnan(行))]
通过迭代所有行,可以显示具有空