I have N (100-1K) of huge Dataframes (100GB - 1T each) and need to process each of them and get a small dataframe (couple of MB) and union the processed dataframes. Is there a more optimal way of doing it that this:
df_merged = new_empty_df
for i in range(N):
df_raw = read(i)
df_processed = process(df_raw)
df_merged = df_merged.union(df_processed) # should .persist() be used here?
# output processed
df_merged.write(somewhere)
Also, should .persist() be used in the 3rd line of the loop to speed things up?
Edit 1:
Here' more information about the data and what's happening inside the process.
Data is tsv/parquet containing GPS locations:
| Location |
|---|
| POINT (30 10) |
| POINT (33.44 -22.4) |
| POINT (33.11 -21.7) |
The process first calculates AreaId for each Location point. It's applied to every row.
| Location | AreaId |
|---|---|
| POINT (30 10) | 2 |
| POINT (33.44 -22.4) | 4 |
| POINT (33.11 -21.7) | 4 |
Then, I need want to calculate the number of points inside each AreaId, resulting in the following:
| AreaId | Count |
|---|---|
| 2 | 1 |
| 4 | 2 |
The code I used for those 2 things is:
df_area_id= df_raw.withColumn('AreaId',GetTileForPointUDF(col('Location')))
df_processed = df_area_id.groupby('AreaId').count()