3

Is there any way to load data directly into an Oracle table from Pandas.

Currently I'm writing the data set into a csv file and then loading the table. I would like to bypass "writing to csv" step.

I'm using cx_Oracle for connecting to Oracle database. url is passed as a parameter when calling the python script. result will be stored as a pandas dataframe in variable dataset . The layout of dataset and table definition are same.

    import cx_Oracle as cx
    response = requests.get(url)
    data = response.json()
    dataset = json_normalize(data['results'])

Please let me know if you require any further .

4
  • 1
    Pretty much every operation in every data pipe line entails generating a CSV file and passing it to the next step. Commented May 2, 2018 at 10:21
  • @APC when you just need to perform some operation you can read directly from DB, or source. Commented May 2, 2018 at 10:24
  • @harsh - I was being - or trying to ba - humourous. Apologies for any confusion caused. Commented May 2, 2018 at 10:28
  • Apologies if there was any confusions.. The question is about write to Oracle from pandas dataframe and not reading from Oracle. Currently, I've been writing it to csv file and then to Oracle. But I would like to know if I can bypass this step. It would be great if I can bypass it for some of the data loads. For most processes in the pipeline, we are writing it to csv. Commented May 2, 2018 at 10:37

1 Answer 1

3

Did you try to_sql function from pandas module?

from sqlalchemy import create_engine
engine = create_engine('oracle://[user]:[pass]@[host]:[port]/[schema]', echo=False)
dataset.to_sql(name='target_table',con=engine ,if_exists = 'append', index=False)
Sign up to request clarification or add additional context in comments.

4 Comments

No the question is write to Oracle (Load table from Pandas)
@:harsh - actually the question is about writing from pandas to Oracle table. So this seems like a reasonable response.
@F.J I tried it, but it says "InvalidRequestError: Could not reflect: requested table(s) not available in Engine" ; But name of the table is present and same, because first I received another message with invalid identifier : "DatabaseError: (cx_Oracle.DatabaseError) ORA-00904: "population": invalid identifier" and I corrected the name of the column.
My bad. It is working when I put if_exists = 'append', I thought I could truncate and reload if I put if_exists = 'replace' which seems to be not working.

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.