My pandas data frame contains several columns, some of them have missing values which show up as a ? sign. I want to run a for loop to print how much ? there is in each columns of the data. I’m doing something like this:
colnames = ['col_1','col_2','col_3']
for i in colnames:
print(f'In the {i} feature, the value - ? - occurs {data.i.value_counts()["?"]} times')
The error I get is :
AttributeError: 'DataFrame' object has no attribute 'i'
So I think that problem is with this part – data.i.value_counts(), I tried data[i].value_counts() but that didn’t work eaither..
>Solution :
For count values avoid value_counts, because failed selecting ? if value not exist in column. Simplier is compare values by ? and count Trues by sum:
for i in colnames:
print(f'In the {i} feature, the value - ? - occurs {data[i].eq("?").sum()} times')