小编Pin*_*ng 的帖子

将 Pandas 中的多个值替换为缺失值(无)

我有一个数据集d,其中包含不同形式的缺失值:

 d = {'col1': [1, 2, '', 'N/A', 'unknown', None], 
      'col2': [3, 4, 'N/A', None, 'N/A_N/A', '']}
d = pd.DataFrame(data=d)

          col1     col2
0        1        3
1        2        4
2               N/A
3      N/A     None
4  unknown  N/A_N/A
5     None 
Run Code Online (Sandbox Code Playgroud)

我想看看每列中实际上缺少多少个值。因此,我想将所有空白、n/a 和未知数转换为None。我尝试了这段代码并得到以下结果:

d.replace(to_replace =['N/A', '', 'unknown', 'N/A_N/A'],  
                            value = None)

   col1  col2
0     1     3
1     2     4
2     2     4
3     2  None
4     2  None
5  None  None 
Run Code Online (Sandbox Code Playgroud)

我不明白为什么d.replace会这样,有人有更好的解决方案来解决我的问题吗?我希望它是这样的:

     col1     col2
0        1        3 …
Run Code Online (Sandbox Code Playgroud)

python pandas

4
推荐指数
1
解决办法
1724
查看次数

标签 统计

pandas ×1

python ×1