I have a pandas dataframe
containing different datatypes, like the one below:
import pandas as pd
df = pd.DataFrame([[1, 'a', (4,5,6)],[1, 'b', (7,8,9)]])
print(df)
# 0 1 2
# 0 1 a (4, 5, 6)
# 1 1 b (7, 8, 9)
print(df.dtypes)
# 0 int64
# 1 object
# 2 object
# dtype: object
I want to save df
to a MySql database using sqlalchemy and possibly pandas' to_sql()
function.
The problem is that trying df.to_sql(table_name, conn)
raises an exception because mysql connector doesn't nkow how to convert the tuple to a mysql type.
As a workaround, I turned the tuple in a string by using apply()
, adding quotes around the tuples. This solution works and the tuple is written in the db as a string.
This is not ideal because when I read the table from the db I will have to convert strings containing a tuple back to tuples, which in my opinion is not the most elegant solution.
My question is: is there a better way to accomplish this?
Looking around I can't seem to find any simple solution to this problem
By the way, what I'm trying to do is to store sklearn model parametrizations in a db. The tuple problem arises when using MLPRegressor
which wants a tuple for hidden_layer_sizes
argument