Writing JSON column to Postgres using Pandas .to_sql

I’ve been searching the web for a solution but couldn’t find any so here is what we came up with (there might be better ways but at least this is a start if someone else runs into this). Specify the dtype parameter in to_sql. We went from:df.to_sql(table_name, analytics_db) to df.to_sql(table_name, analytics_db, dtype={‘name_of_json_column_in_source_table’: sqlalchemy.types.JSON}) and it … Read more

Randomly insert NA’s values in a pandas dataframe

Here’s a way to clear exactly 10% of cells (or rather, as close to 10% as can be achieved with the existing data frame’s size). import random ix = [(row, col) for row in range(df.shape[0]) for col in range(df.shape[1])] for row, col in random.sample(ix, int(round(.1*len(ix)))): df.iat[row, col] = np.nan Here’s a way to clear cells … Read more

‘XlsxWriter’ object has no attribute ‘save’. Did you mean: ‘_save’?

The save() method has been deprecated and removed in Pandas. You should use close() instead. With older versions of Pandas you would have gotten this warning: FutureWarning: save is not part of the public API, usage can give unexpected results and will be removed in a future version With more recent versions you just need … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)