Save pandas dataframe but conserving NA values

不问归期 提交于 2019-11-27 18:25:53

问题


I have this code

import pandas as pd
import numpy as np
import csv
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}); 

And I changed the weight with NA values:

df['weight'] = np.nan

And finally I saved it

df.to_csv("ejemplo.csv", sep=";", decimal=",", quoting=csv.QUOTE_NONNUMERIC, index=False)

But when I read the file I have "", instead of NA I want to put NA instead of Nan

I want as output:

adult;animal;size;weight
False;"dog";"S";NA
False;"cat";"M";NA    

回答1:


To get that specific output, you'll have to pass the quotes in explicitly.

df = pd.DataFrame({'animal': r'"cat" "dog" "cat" "fish" "dog" "cat" "cat"'.split(),
           'size': list(r'"S" "S" "M" "M" "M" "L" "L"'.split()),
           'weight': [8, 10, 11, 1, 20, 12, 12],
           'adult' : [False] * 5 + [True] * 2}); 
df['weight'] = '%s' %('NA')
df.to_csv("ejemplo.csv", sep=';', decimal=',',quoting=csv.QUOTE_NONE, index=False)



回答2:


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\n0,False,cat,S,NA\n1,False,dog,S,NA\n2,False,cat,M,NA\n3,False,fish,M,NA\n4,False,dog,M,NA\n5,True,cat,L,NA\n6,True,cat,L,NA\n'

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\n0,False,cat,S,'NA'\n1,False,dog,S,'NA'\n2,False,cat,M,'NA'\n3,False,fish,M,'NA'\n4,False,dog,M,'NA'\n5,True,cat,L,'NA'\n6,True,cat,L,'NA'\n"

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\nFalse;cat;S;NA\nFalse;dog;S;NA\nFalse;cat;M;NA\nFalse;fish;M;NA\nFalse;dog;M;NA\nTrue;cat;L;NA\nTrue;cat;L;NA\n'


来源:https://stackoverflow.com/questions/36405994/save-pandas-dataframe-but-conserving-na-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!