Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Changing the format of NaN values when saving to CSV

Tags:

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.

like image 832
Jerry Avatar asked Jun 16 '18 19:06

Jerry


People also ask

How do I change NaN values with different values in pandas?

You can replace the missing value ( NaN ) in pandas. DataFrame and Series with any value using the fillna() method.

Does Panda read NaN na?

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.

How do I save pandas data to CSV?

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.


1 Answers

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 
like image 167
cs95 Avatar answered Oct 06 '22 00:10

cs95