-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpandas_etl.py
More file actions
75 lines (55 loc) · 2.07 KB
/
Copy pathpandas_etl.py
File metadata and controls
75 lines (55 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import boto3
import pandas as pd
from io import StringIO, BytesIO
# create s3 client
s3 = boto3.client('s3')
# specify the bucket & folder you'd like to interact with
name = 'tkh-nyc-education'
folder = 'data/'
# view all objects within this folder
response = s3.list_objects_v2(Bucket=name, Prefix=folder)
print("\n Multiple object retrieval \n")
####
# NOTE: The below code is for when you have multiple objects
# Example code: concatenate all files together
concat_list = []
# Interacting with multiple objects
# loop through all objects, handle them as a data frame
for file in response['Contents']:
fname = file['Key']
# if this is a csv file...
if fname.endswith('.csv') or fname.endswith('.xlsx'):
# get csv object
object = s3.get_object(Bucket=name, Key=fname)
print(fname)
if fname.endswith('.csv'):
df = pd.read_csv(object['Body'],low_memory=False)
concat_list.append(df)
elif fname.endswith('.xlsx'):
object = s3.get_object(Bucket=name, Key=fname)
excel_data = object['Body'].read()
excel_file = BytesIO(excel_data)
excel = pd.ExcelFile(excel_file)
# for sheet_name in excel.sheet_names:
# df = pd.read_excel(excel,sheet_name=sheet_name,engine='openpyxl')
# csv_file = f'{sheet_name}.csv'
# df = df.to_csv(csv_file,index=False)
# concat_list.append(df)
#print(concat_list)
# concatenate all files together
hpot_data = pd.concat(concat_list)
# verify columns
#print(hpot_data.columns)
# verify shape
#print(hpot_data.shape)
# Lastly, after we are done performing our transformations, we load this data back into our bucket
# create an empty buffer to prepare our data push
buffer = StringIO()
# load our dataframe into this placeholder
hpot_data.to_csv(buffer, index=False)
# push this buffer to our s3 data store
s3.put_object(
Bucket='tkh-nyc-education',
Key='transformed_file.csv',
Body=buffer.getvalue()
)