I have a .csv file with some empty cells. How can I count empty cells in .csv file? if row['PredictionString']== " "
?
submission = pd.read_csv(os.path.join(ROOT_DIR, 'submission.csv'))
for index, row in submission.iterrows():
if row['PredictionString']== " ":
counter1 = counter1 + 1
print('output:', counter1)
It doesn't work.
output: 0
First col name: patientId
Second col name: PredictionString
If submission
is a pandas dataframe (like it seems to be), you can count like this:
counter1 = len(submission[submission.PredictionString == ' '])
Without any for loops.
EDIT: Considering as empty ' '
, ''
and NaN
's:
counter1 = len(submission[(submission.PredictionString == ' ') | (submission.PredictionString == '') | (submission.PredictionString.isnull())])
Example:
>> mydict = {'patientId': {0: '1', 1: '1', 2: '1'},
>> 'PredictionString': {0: '', 1: ' ', 2: np.NaN}}
>> submission = pd.DataFrame(mydict)
>> counter1 = len(submission[(submission.PredictionString == ' ') | (submission.PredictionString == '') | (submission.PredictionString.isnull())])
>> print(counter1)
3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.