I have to import data from Excel files into the database. The structure of these files does not match with the structure of the model, so I guess I need to do some kind of data-manipulation to arrange the tuples accordingly.
The files I have to import look like this:
The django-model has the following attributes: Country, Commodity, Year, Value
So what would be the best way to read the data, arrange it in the correct structure and import it into the database (preferably with automatic updates of existing tuples).
I spent a lot of time researching the existing python- and django-libraries for this requirements (like PyExcel, Pandas, Django-Excel, Django-Import-Export), but I couldn't really find out which is the best and if it supports the rearrangement of the data before importing.
I hope you can give me some recommendations and solutions for this task.
If your data is an Excel file, it's structured. The nature of a table is to structure data. Unstructured data would be something like a text file or a directory of text files.
You can use Python's pandas library to import Excel files, manipulate them, and upload them to SQL databases.
input:
output:
If you have many Excel files, you can use
glob
oros.walk
to iterate through/over a directory, and import some or all of the Excel files.You can use pandas
DataFrame.to_sql
to upload your data to an SQL database, which requires defining the database connection (server address, login/pw, etc.).