Insert data into Postgresql using Dataframe Pandas

Asked

Viewed 190 times

0

Prazados,

I am working on a particular project in which I want to do an ETL with Python and Postgresql. I intended to consume a GET API using request, persist this data in a Dataframe and after that, persist the data (without treating) in an existing table in Postgresql. I’m using python 3.7.4

Follows script used:

import requests
import pandas as pd
import urllib, json
from sqlalchemy import create_engine

engine = create_engine('postgresql://sa:123@localhost:5432/db')

with urllib.request.urlopen("URL QUE VOU CONSUMIR") as url:

data = json.loads(url.read().decode())

df = pd.DataFrame(data)

newdf = pd.DataFrame(df, columns = ['<column_name1>,<column_name2>, ...'])

newdf.to_sql('<table_name>', con=engine, if_exists='append')

The current problem is to persist the data in a table in Postgresql, given that I have the data in Dataframe.

Can someone help me?

  • returned some error?

  • No, no error returned, ran flawlessly, however, did not insert the records in postgresql

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.