dacc_final=pd.read_csv("dacc_out.csv")
dacc_features=['DACC_1460', 'DACC_1556','DACC_1938','DACC_2013','DACC_2798','DACC_2795']
dacc_final=dacc_final[dacc_features]
dacc_final=dacc_final.reset_index(drop=True)
final_df=pd.concat([df_first,dacc_final],axis=1)
dcc_final=pd.read_csv("dcc_out.csv")
dcc_features=['DCC_1384','DCC_1580','DCC_1862','DCC_1937','DCC_2135','DCC_2719','DCC_2722']
dcc_final=dcc_final[dcc_features]
dcc_final=dcc_final.reset_index(drop=True)
final_df=pd.concat([final_df,dcc_final],axis=1)
ddon_final=pd.read_csv("ddon_out.csv")
ddon_final=ddon_final['DDON_G'] # This assumes 'DDON_G' is the only column needed or exists.
ddon_final=ddon_final.reset_index(drop=True)
final_df=pd.concat([final_df,ddon_final],axis=1)
mac_final=pd.read_csv("mac_out.csv")
mac_final=mac_final['MAC_54']
mac_final=mac_final.reset_index(drop=True)
final_df=pd.concat([final_df,mac_final],axis=1)
nmbac_final=pd.read_csv("nmbac_out.csv")
nmbac_final=nmbac_final[['NMBAC_47','NMBAC_64']]
nmbac_final=nmbac_final.reset_index(drop=True)
final_df=pd.concat([final_df,nmbac_final],axis=1)
pcp_final=pd.read_csv("pcptnc_out.csv")
pcp_final=pcp_final[['PC_PTNC_19','PC_PTNC_42','PC_PTNC_46']]
pcp_final=pcp_final.reset_index(drop=True)
final_df=pd.concat([final_df,pcp_final],axis=1)
Make each of those parallel, Wait for all to finish, concat in same order.
Assistant
To make the operations parallel and wait for all to complete before concatenating, we can use Python's concurrent.futures.ThreadPoolExecutor or multiprocessing.Pool. Since reading CSV files is mostly I/O-bound, ThreadPoolExecutor is more appropriate. Here's the parallel version of your code:
python
import pandas as pd
from concurrent.futures import ThreadPoolExecutor
# Define a function to handle the reading and filtering of each CSV
def load_and_filter_csv(file_path, features=None, single_feature=None):
df = pd.read_csv(file_path)
if features: # For cases with multiple features
df = df[features]
elif single_feature: # For cases with a single feature
df = df[single_feature]
return df.reset_index(drop=True)
# Define the tasks to run in parallel
tasks = [
lambda: load_and_filter_csv("dacc_out.csv", features=['DACC_1460', 'DACC_1556', 'DACC_1938', 'DACC_2013', 'DACC_2798', 'DACC_2795']),
lambda: load_and_filter_csv("dcc_out.csv", features=['DCC_1384', 'DCC_1580', 'DCC_1862', 'DCC_1937', 'DCC_2135', 'DCC_2719', 'DCC_2722']),
lambda: load_and_filter_csv("ddon_out.csv", single_feature='DDON_G'),
lambda: load_and_filter_csv("mac_out.csv", single_feature='MAC_54'),
lambda: load_and_filter_csv("nmbac_out.csv", features=['NMBAC_47', 'NMBAC_64']),
lambda: load_and_filter_csv("pcptnc_out.csv", features=['PC_PTNC_19', 'PC_PTNC_42', 'PC_PTNC_46'])
]
# Run tasks in parallel and wait for all to complete
with ThreadPoolExecutor() as executor:
# Submit all tasks and get futures
futures = [executor.submit(task) for task in tasks]
# Wait for all futures to complete and get results in order
results = [future.result() for future in futures]
# Concatenate results in the same order, starting with df_first
final_df = pd.concat([df_first] + results, axis=1)
Explanation:
Function Definition (load_and_filter_csv):
This function handles the reading of a CSV file and filtering it to the desired columns (either multiple features or a single feature).
It ensures the index is reset for consistency.
Tasks List:
Each task is defined as a lambda function that calls load_and_filter_csv with the appropriate arguments.
This ensures that the operations are independent and can run in parallel.
Parallel Execution:
ThreadPoolExecutor is used to run the tasks in parallel.
executor.submit(task) submits each task to the thread pool, returning a Future object.
The results are collected in the order of the tasks list using [future.result() for future in futures].
Concatenation:
The results are concatenated with df_first in the same order as the original code.
Using pd.concat([df_first] + results, axis=1) ensures the order is preserved.
Benefits:
Speed: Reading multiple CSV files in parallel can significantly reduce the total execution time, especially if the files are large or stored on a slow disk.
Order Preservation: The results are collected in the same order as the tasks, ensuring the final DataFrame is identical to the sequential version.
Simplicity: The code structure remains similar to the original, making it easy to understand and maintain.
Notes:
Ensure df_first is defined before running this code, as it is assumed to exist in your original code.
If the CSV files are very large, you might want to consider memory usage and potentially switch to multiprocessing.Pool for true parallelism (though this is less likely to be necessary for I/O-bound tasks).
If you need to handle exceptions (e.g., if a file is missing), you can add error handling within the load_and_filter_csv function or around the future.result() calls.