mercredi 30 septembre 2015

Merging and averaging tables in a database (Python)

I would like to merge and average the values in different database tables using Python. My database database.db contains tables named in the following way: 'STATE'_'TOWN'. For example, some table names are:

CALIFORNIA_SANFRAN

CALIFORNIA_LOSANGELES

CALIFORNIA_SANDIEGO

...

To illustrate, the CALIFORNIA_SANFRAN table contains the following (example) data:

Date (PRIMARY KEY)      Morning    Day    Evening   Night
01.01.2014              0.5        0.2     0.2      0.1
02.01.2014              0.6        0.1     0.2      0.2
...

and the CALIFORNIA_LOSANGELES table contains:

Date (PRIMARY KEY)      Morning    Day    Evening   Night
01.01.2014              0.3        0.4     0.4      0.3
02.01.2014              0.4        0.3     0.1      0.2
...

ect.

From these tables I would like to obtain one table named after the State (CALIFORNIA) in a separate database (let's call it database2.db) with the averages of all the values in tables CALIFORNIA_SANFRAN, CALIFORNIA_LOSANGELES, CALIFORNIA_SANDIEGO for each date. I.e. I want database2.db, table 'CALIFORNIA' to contain something like:

 Date (PRIMARY KEY)      Morning    Day    Evening   Night
    01.01.2014              0.4     0.3     0.3      0.2
    02.01.2014              0.5     0.2     0.15     0.2
    ....

I haven't been able to find anything that I can use online so far so any help would be greatly appreciated.

PS. I've used and am most familiar with using SQLite to work with databases from Python.

Aucun commentaire:

Enregistrer un commentaire