简体   繁体   中英

Create postgres table from dictionary in python

I'm using the dataset package in python to connect to a postgres database on my local machine. After connecting, I run the following code:

db = dataset.connect('postgresql://user:password@localhost:5432/db')

my_dict = {'Person': ['Joe', 'Lou', 'Kim', 'Tim', 'Emma'], 
           'Age': [40, 37, 13, 8, 3], 
           'Height': ["5'11", "5'6", "5'8", "4'3", "3'0"]}

table = db['new_data']
table.insert(my_dict)

This creates a table called new_data in my local database, but the results come out like this:

 id |             Person             |      Age       |         Height        
----+--------------------------------+----------------+------------------------
  1 |      {Joe,Lou,Kim,Tim,Emma}    | {40,37,13,8,3} | {5'11,5'6,5'8,4'3,3'0}

Essentially, all the values of my dictionary item come back on the same row. This should have a different row from each item, similar to a dataframe.

    Person  Age Height
0   Joe     40  5'11
1   Lou     37  5'6
2   Kim     13  5'8
3   Tim     8   4'3
4   Emma    3   3'0

I couple things I tried:

I created my dictionary manually {k:v} . This works when I pass that object to insert it into the table, but it makes the rows incorrect like you see above. I also tried using the to_dict function, creating the dictionary from a pandas DataFrame, but I get the following error:

ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'dict'

This seems to have something to do with how the dictionary item is created when using the to_dict function, as the key is the column name, but the values are a nested dictionary, with row indices as the key and the row values as values.

The other thing I tried was created the dictionary using a dictionary comprehension, iterating over the dataframe. I get the same error as above. I have no idea how to fix this.

Assuming all the keys inside your dict are of equal length, you can convert your above dictionary to the below form.

[{'Person': 'Joe', 'Age': 40, 'Height': "5'11"},
 {'Person': 'Lou', 'Age': 37, 'Height': "5'6"},
 {'Person': 'Kim', 'Age': 13, 'Height': "5'8"},
 {'Person': 'Tim', 'Age': 8, 'Height': "4'3"},
 {'Person': 'Emma', 'Age': 3, 'Height': "3'0"}]

Now you can iterate and insert each dict object into your table as shown below,

for pos, val in enumerate(my_dict['Person']):
    table.insert({'Person': val, 'Age': my_dict['Age'][pos], 'Height': my_dict['Height'][pos]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM