Is it possible that a dataframe that has empty strings for values in a column will be converted to null?

Viewed 422

So some values in one of the columns in my dataframe are ''. When I save the df as a CSV using .to_csv() to use it in another function I get a csv with null values.

1 Answers

Use replace:

df.replace('', 'null').to_csv(file)
#if need specify only some column
df.replace({'A': {'': 'null'}}).to_csv(file)

If empty values are NaNs add parameter na_rep for convert all NaNs to another value:

df.to_csv(file, na_rep='null')
Related