1

I have a API wrapper that pulls data from a specific product. I am facing this problem of how can I map the json data to the database (postgresql). I have read up on Pandas dataframe but I am unsure if it is the right way to go. I have a few questions that I need help with.

1) Is it possible to choose which rows get into the dataframe?

2) Every row inside the dataframe needs to be inserted into two different database tables. I would need to insert ten columns into TableA get the id of the newly inserted row and insert five columns including the returned id into TableB. How would I go about this?

3) Is it possible to specify the data types for each column in the dataframe?

4) Is it possible to rename the column names to the database field names?

5) Is it possible to iterate through specific columns and replace certain data?

Is there a specific term for what I am trying to accomplish which I can search for?

Many thanks!

2
  • 1
    Have you seen these? read_sql_query and to_sql Commented May 14, 2020 at 19:56
  • Thanks for the reply, I will look it up. Commented May 14, 2020 at 23:31

1 Answer 1

3

1) Yes, you can. You can follow this tutorial

2) You can achieve this following the same tutorial as before.

3) Theres 3 main options to convert data types in pandas:

3.1) to_numeric() - provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().)

3.2) astype() - convert (almost) any type to (almost) any other type (even if it's not necessarily sensible to do so). Also allows you to convert to categorial types (very useful).

3.3) infer_objects() - a utility method to convert object columns holding Python objects to a pandas type if possible.

4) You can simply call the .rename function as explained here

5) Theres at least 5 ways to iterate over data in pandas. Some are faster than others, but the ideal way depends for each case. There's a very good post on GeeksForGeeks about it.

I Hope I could help you somehow =)

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.