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 oppose to being NULL.
I have looked around but can't find a workable solution. Here's the basic issue:
df index x y z 0 1 NaN 2 1 NaN 3 4
CSV output:
index x y z 0 1 nan 2 1 nan 3 4
I have tried a few things to set 'nan' to NULL but the csv output results in a 'blank' rather than NULL:
dfDemographics = dfDemographics.replace('nan', np.NaN) dfDemographics.replace(r'\s+( +\.)|#', np.nan, regex=True).replace('', np.nan) dfDemographics = dfDemographics.replace('nan', '') # of course, this wouldn't work, but tried it anyway.
Any help would be appreciated.
You can replace the missing value ( NaN ) in pandas. DataFrame and Series with any value using the fillna() method.
by-default pandas consider #N/A, -NaN, -n/a, N/A, NULL etc as NaN value. let's see the example for better understanding. so this is our dataframe it has three column names, class, and total marks. now import the dataframe in python pandas.
Exporting the DataFrame into a CSV filePandas DataFrame to_csv() function exports the DataFrame to CSV format. If a file argument is provided, the output will be the CSV file. Otherwise, the return value is a CSV format like string. sep: Specify a custom delimiter for the CSV output, the default is a comma.
Pandas to the rescue, use na_rep
to fix your own representation for NaNs.
df.to_csv('file.csv', na_rep='NULL')
file.csv
,index,x,y,z 0,0,1.0,NULL,2 1,1,NULL,3.0,4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With