Pandas Changing the format of NaN values when saving to CSV

后端 未结 4 2325
南笙
南笙 2021-02-19 06:55

I am working with a df and using numpy to transform data - including setting blanks (or \'\') to NaN. But when I write the df to csv - the output contains the string \'nan\' as

4条回答
  •  情书的邮戳
    2021-02-19 07:46

    User @coldspeed illustrates how to replace nan values with NULL when save pd.DataFrame. In case, for data analysis, one is interested in replacing the "NULL" values in pd.DataFrame with np.NaN values, the following code will do:

    import numpy as np, pandas as pd
    
    # replace NULL values with np.nan
    colNames = mydf.columns.tolist()
    dfVals = mydf.values
    matSyb = mydf.isnull().values
    dfVals[matSyb] = np.NAN
    
    mydf = pd.DataFrame(dfVals, columns=colNames)    
    #np.nansum(mydf.values, axis=0 )
    #np.nansum(dfVals, axis=0 )
    

提交回复
热议问题