我如何计算像'这样的特殊字符?'对于我的DataFrame中的Pandas中的每一列?

问题描述 投票:0回答:2

这似乎是一个容易和简单的任务,但是我正在寻找基本而全面的答案,以对我的缺失值进行计数,以将它们编码为'?'字符。

我的数据:enter image description here

我希望我的答案像这样:

drive_wheels 0
engine_location 0
engine_type 0
num_of_cylinders 0
fuel_system 0
bore 4
stroke 4

我尝试过这个:

 for i in data.columns:
           counter = 0
           if data[i].dtype == '?':
                counter += 1
           else:
                counter = 1
 print(i, ' ', str(sum(counter)))

如果有人可以帮助我,我将不胜感激。谢谢!

python pandas special-characters
2个回答
0
投票

您将计数器变量包含在循环中,这意味着您将每个循环将其重置为0。您要做的就是像这样将其移到循环外;

counter = 0
for i in data.columns:
    if data[i].dtype == '?'
        counter += 1

0
投票

[Apply函数到每列Apply equal并求和equal结果的每一列

?

如果由于某种原因内容不严格等于True(例如,它包含其他空格),请使用data.apply(lambda serie: serie.eq('?').sum(), axis=0) 方法:

?
© www.soinside.com 2019 - 2024. All rights reserved.