I have 2 DataFrame.
import pandas as pd
loc_hour = pd.DataFrame({'id': ['a', 'b', 'c',"d"], 'geohash': ["sybewp", "sws101", "sxk9db","sxr4xt"],"log_date":[20210615,20211219,20210108,20210507],"hour":[12,4,5,19]})
loc_grid = pd.DataFrame({'id': ['a', 'b', 'c',"d"], 'geohash': ["sybewp", "sws101", "sxk9db","sxr4xt"], "gridtype":["Home","Other","Work","Home"]})
These tables contain thousands of different devices.
The loc_hour table contains current location information from devices every hour for a whole year.
In the loc_grid table, there are attributes of the locations where the devices are located. (Home: indicates that the device is at home. Work: indicates that the device is at home.)
What I'm trying to do is calculate how long the devices spend in their home, work or other place as a percentage of the long term (3 months) and short term (1 month).
Desired outputs:
| id | home_percent | work_percent | other_percent | total_day_count | total_hour_count | period |
|---|---|---|---|---|---|---|
| a | 40 | 40 | 20 | 5 | 5 | last 1 month |
| b | 50 | 30 | 20 | 10 | 8 | last 1 month |
| c | 70 | 20 | 10 | 9 | 7 | last 1 month |
| ... | ... | ... | ... | ... | ... | ... |
| id | home_percent | work_percent | other_percent | total_day_count | total_hour_count | period |
|---|---|---|---|---|---|---|
| a | 40 | 40 | 20 | 5 | 5 | last 3 month |
| b | 50 | 30 | 20 | 10 | 8 | last 3 month |
| c | 70 | 20 | 10 | 9 | 7 | last 3 month |
| ... | ... | ... | ... | ... | ... | ... |