简体   繁体   中英

How to replace nan and null values?

I wrote the following code that takes a column from a csv file and gets rid of any non integer values in a cell.

    print("before:"+str(listname[i]))
    num = listname[i]
    if pd.isna(listname[i]):
        num = 0
    else:
        num = num

    num = re.sub("[^\d\.]", "", str(num))
    if math.isnan(num):
        num = 0
    else:
        num = num

    print("after:"+str(num))
    return num,listname,i

If the value of the cell happens to be nan/null it skips the file however I want it to take take the null/nan value of a cell as 0 and continue with the calculation. Is there any simple way to do this? please tell me if any more information is needed.

there are two methods, One way to do it is to use pd.dropna() which will take the mean of the values and fill it in with that. The other way is pd.fillna(value) where values is the value that you specify. Hope this answers your question!

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM