Python - Ignore Nan values in a . csv and add the rest

Asked

Viewed 98 times

0

Hello,

I have a file . csv that I am trying to pass to json

Produtos      Quantidade
    Bala          50
    Refri         NaN
    Salgado       25

And I’m importing for a json, wanted to know how to ignore the Nan values and return the sum.

This is the code:

df = pd.read_csv(r"quantida.csv", sep=";") 

totalQuantidade = sum(data['Quantidade'])

dataJSON = {
        "Volume": totalQuantidade
    }
#Escrever no JSON
with open('estoque.json', 'w') as jsonFile:
       jsonFile.write(json.dumps(dataJSON, indent=4))
print('Concluído')  

1 answer

0


Using the method sum, you can specify the parameter value skipna to exclude NaNs:

totalQuantidade = df['Quantidade'].sum(skipna=True)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.