Save pandas dataframe but conserving NA values

后端 未结 2 1003
鱼传尺愫
鱼传尺愫 2020-12-16 19:57

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(),
               \'         


        
2条回答
  •  时光说笑
    2020-12-16 20:28

    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)
    

提交回复
热议问题