If you want a string to represent NaN
values then pass na_rep
to to_csv
:
In [8]:
df.to_csv(na_rep='NA')
Out[8]:
',adult,animal,size,weight
0,False,cat,S,NA
1,False,dog,S,NA
2,False,cat,M,NA
3,False,fish,M,NA
4,False,dog,M,NA
5,True,cat,L,NA
6,True,cat,L,NA
'
If you want the NA
in quotes then escape the quotes:
In [3]:
df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(),
'size': list('SSMMMLL'),
'weight': [8, 10, 11, 1, 20, 12, 12],
'adult' : [False] * 5 + [True] * 2})
df['weight'] = np.NaN
df.to_csv(na_rep=''NA'')
Out[3]:
",adult,animal,size,weight
0,False,cat,S,'NA'
1,False,dog,S,'NA'
2,False,cat,M,'NA'
3,False,fish,M,'NA'
4,False,dog,M,'NA'
5,True,cat,L,'NA'
6,True,cat,L,'NA'
"
EDIT
To get the desired output use these params:
In [27]:
df.to_csv(na_rep='NA', sep=';', index=False,quoting=3)
?
Out[27]:
'adult;animal;size;weight
False;cat;S;NA
False;dog;S;NA
False;cat;M;NA
False;fish;M;NA
False;dog;M;NA
True;cat;L;NA
True;cat;L;NA
'