0

I am saving a dataframe to sql using the following:

df.astype({...}).to_sql('search_raw', con=self.avails_conn, index='id')

I have a few list fields, and where the entry is None, instead of saving it as NULL, it saves it as the string nan. For example: enter image description here

How can I save a list-field so that a null value (not [] but None) is saved as NULL in sql rather than "nan" ?


It seems that this approach doesn't work for a list field: https://stackoverflow.com/a/54403705/651174.


This might be a bit crude, but here's how I'm doing it now:

df['all_audio_languages'] = np.where(df['all_audio_languages'] == 'nan', None, df['all_audio_languages'])

Is there a better way to do this? The above approach seems pretty hackish.

David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

0

Have you tried?

df.astype({...}).to_sql('search_raw', con=self.avails_conn, index='id')
df = df.fillna(None)