-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetl_pandas.py
54 lines (41 loc) · 1.94 KB
/
etl_pandas.py
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
import os
import pandas as pd
from multiprocessing import Pool, cpu_count
from tqdm import tqdm # importa o tqdm para barra de progresso
CONCURRENCY = cpu_count()
# Usando os.path.join para garantir compatibilidade entre sistemas operacionais
PATH_DO_TXT = os.path.join("data", "measurements.txt")
total_linhas = 1_000_000_000 # Total de linhas conhecido
chunksize = 100_000_000 # Define o tamanho do chunk
# Atribui o valor de PATH_DO_TXT para filename
filename = PATH_DO_TXT
def process_chunk(chunk):
# Agrega os dados dentro do chunk usando Pandas
aggregated = chunk.groupby('station')['measure'].agg(['min', 'max', 'mean']).reset_index()
return aggregated
def create_df_with_pandas(filename, total_linhas, chunksize=chunksize):
total_chunks = total_linhas // chunksize + (1 if total_linhas % chunksize else 0)
results = []
with pd.read_csv(filename, sep=';', header=None, names=['station', 'measure'], chunksize=chunksize) as reader:
# Envolvendo o iterador com tqdm para visualizar o progresso
with Pool(CONCURRENCY) as pool:
for chunk in tqdm(reader, total=total_chunks, desc="Processando"):
# Processa cada chunk em paralelo
result = pool.apply_async(process_chunk, (chunk,))
results.append(result)
results = [result.get() for result in results]
final_df = pd.concat(results, ignore_index=True)
final_aggregated_df = final_df.groupby('station').agg({
'min': 'min',
'max': 'max',
'mean': 'mean'
}).reset_index().sort_values('station')
return final_aggregated_df
if __name__ == "__main__":
import time
print("Iniciando o processamento do arquivo.")
start_time = time.time()
df = create_df_with_pandas(filename, total_linhas, chunksize)
took = time.time() - start_time
print(df.head())
print(f"O processamento demorou: {took:.2f} sec")