【pandas】[7] Series 判断每个元素是否为空

有时候需要对Series中的每个元素进行判断,然后做下一步逻辑处理

1、Series是数值类型的时候;里面有空值(np.nan);value_counts()不会统计到空值。判断时需要使用np.isnan(x)

ab = pd.Series([1, np.nan, 2])
ab
Out[55]: 
0    1.0
1    NaN
2    2.0
dtype: float64
ab.map(lambda x: np.isnan(x))
Out[56]: 
0    False
1     True
2    False
dtype: bool
ab.value_counts()
Out[57]: 
2.0    1
1.0    1
dtype: int64

2、Series是数值类型的时候;里面有空值(np.nan);value_counts()不会统计到空值。判断时需要使用x is np.nan

ab = pd.Series(['1', np.nan, '2'])
ab
Out[59]: 
0      1
1    NaN
2      2
dtype: object
ab.map(lambda x: x is np.nan)
Out[60]: 
0    False
1     True
2    False
dtype: bool
ab.value_counts()
Out[61]: 
2    1
1    1
dtype: int64

3、Series是数值类型的时候;里面有空值(None);value_counts()不会统计到空值。判断时需要使用np.isnan(x)

ab = pd.Series([1, None, 2])
ab
Out[67]: 
0    1.0
1    NaN
2    2.0
dtype: float64
ab.map(lambda x: np.isnan(x))
Out[69]: 
0    False
1     True
2    False
dtype: bool
ab.value_counts()
Out[70]: 
2.0    1
1.0    1
dtype: int64

4、Series是类别类型的时候;里面有空值(None);value_counts()不会统计到空值。判断时需要使用x is None

ab = pd.Series(['1', None, '2'])
ab
Out[63]: 
0       1
1    None
2       2
dtype: object
ab.map(lambda x: x is None)
Out[64]: 
0    False
1     True
2    False
dtype: bool
ab.value_counts()
Out[65]: 
2    1
1    1
dtype: int64

5、Series是类别类型的时候;里面有空值('');value_counts()会统计到空值。判断时需要使用x == ''

ab = pd.Series(['1', '', '2'])
ab
Out[77]: 
0    1
1     
2    2
dtype: object
ab.map(lambda x: x == '')
Out[78]: 
0    False
1     True
2    False
dtype: bool
ab.value_counts()
Out[79]: 
2    1
     1
1    1
dtype: int64

总结:

1、不管Series中存储的是类别型还是数值型。如果里面的空是np.nan或None。使用Series.value_counts()都不会将空统计进来;当里面的空是''或' '。使用Series.value_counts(),会将空值统计进来

2、当Series中存储的是数值型的时候,里面的空是np.nan或None,统一使用np.isnan(x)进行判断

3、当Series中存储的是类别型的时候,里面的空是np.nan的时候,使用x is np.nan判断;里面的空是None的时候,使用x is None判断

4、不管Series中存储的是类别型还是数值型。如果里面的空是''或' '。使用x == '' 或 x.strip() == ''进行判断

猜你喜欢

转载自blog.csdn.net/xiezhen_zheng/article/details/105652433