0

How to store values to mysql database that is a dataframe consisting of lists in every row. Below is the dataframe:

0                    [Nissan Motor, Carlos Ghosn]
1                    [Nissan Motor, Carlos Ghosn]
2                                              []
3                              [David Muir, Trio]
4                                              []
5                                              []
6                                              []
1
  • Do you want comma-separated values to go in different columns or single column? Commented Nov 26, 2018 at 9:03

1 Answer 1

1

Something like this?

from sqlalchemy.dialects import postgresql
x = [["Nissan Motor", "Carlos Ghosn"], ["Nissan Motor", "Carlos Ghosn"], [], ["David Muir", "Trio"], [], [],[]]
df = pd.DataFrame({"data": x})

df.to_sql(con = "connection_string", name = "table_name", schema = "schema_name", if_exists="replace", dtype={'data': postgresql.JSONB}, index = False)

Reading it back:

q = "select * from schema.table_name"
df = pd.read_sql(q, con="connection_string")
print(df)

Output:

                           data
0  [Nissan Motor, Carlos Ghosn]
1  [Nissan Motor, Carlos Ghosn]
2                            []
3            [David Muir, Trio]
4                            []
5                            []
6                            []

Used JSONB

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.