So I have a dataset spread over multiple and ever-growing excel files all of which looks like:
order_ID | order_date | |
---|---|---|
[email protected] | 1234 | 23-Mar-2021 |
[email protected] | 1235 | 23-Mar-2021 |
[email protected] | 1236 | 23-Mar-2021 |
[email protected] | 1237 | 24-Mar-2021 |
[email protected] | 1238 | 28-Mar-2021 |
End goal is to have two distinct datasets as:
First one being Orders: (Public. For analysis, trading emails with user_IDs for anonymity and marking returning for further analyses)
user_ID | order_ID | order_date | is_returning? |
---|---|---|---|
1 | 1234 | 23-Mar-2021 | 0 |
2 | 1235 | 23-Mar-2021 | 0 |
2 | 1236 | 23-Mar-2021 | 1 |
1 | 1237 | 24-Mar-2021 | 1 |
3 | 1238 | 28-Mar-2021 | 0 |
Second one being: Users (Private for retaining users_info. Have other columns besides email as well but idea is the same)
user_ID | |
---|---|
1 | [email protected] |
2 | [email protected] |
2 | [email protected] |
1 | [email protected] |
3 | [email protected] |
In pandas, I have following procedure to go about:
Read all files:
input_directory = 'Data/' files = os.listdir(input_directory) files
Combine them:
all_data = pd.DataFrame() for df in data_dict.values(): all_data=pd.concat([all_data,df]).drop_duplicates().reset_index(drop=True) all_data = all_data.drop_duplicates()
Assign user IDs:
all_data['user_id'] = all_data['email'].factorize()[0]
Assign returning flag:
all_data['is_returning'] = all_data.user_id.duplicated().astype(int)
Pushing both to BQ:
columns = #list of private columns all_data.drop(columns, axis = columns).to_gbq(#parameters, table = `public_dataset.orders`) all_data[columns].to_gbq(#parameters, table = `private_dataset.users_db`)
Since files are continually coming, I have to reassign user_IDs again and again. (Because searching a corresponding email for each row in customers db is way more expensive than simply reassigning IDs. All analyses are anonymous and based on total numbers so it wont hurt as well. Also mantaining a local DB is also not preferrable as data is incoming in an excel format) I can have a script to push data once a file is come for only that file but cannot because of
- Not being able to emulate
pd.factorize()
- And
dupicated()
on BigQuery - Not knowing in advance what rows are already there since files may have duplicated records as well,
I have to do this again and again. So how can I transition this whole pipeline to BQ effectively and cost-efficiently? Any help regarding bettering the situation is highly appreciated