im trying to merge some csv files togheter and im getting following error:…
- User
im trying to merge some csv files togheter and im getting following error: numpy.core._exceptions.MemoryError: Unable to allocate 145. MiB for an array with shape (62, 307419) and data type float64
my code: import pandas as pd import os
Directory where all your CSV files are located
csv_directory = r"E:\01_calculate_talib\day_spot"
List all CSV files in the given directory
csv_files = [file for file in os.listdir(csv_directory) if file.endswith('.csv')]
Placeholder for storing the data frames
data_frames = []
Loop over the list of csv files
for csv_file in csv_files: # Read the CSV file and append it to the list of data frames file_path = os.path.join(csv_directory, csv_file) df = pd.read_csv(file_path) data_frames.append(df)
Concatenate all data frames to create a single data frame
combined_df = pd.concat(data_frames, ignore_index=True)
Save the combined data frame to a new CSV file
combined_csv_path = r'C:\Users\Fazel\Desktop\combined_day_stables_1_7_24_4_w_m_labeled.csv' combined_df.to_csv(combined_csv_path, index=False)
print(f'Combined CSV created at: {combined_csv_path}')
- Assistant
- User
update my code so it use option 3. Read in Chunks
- Assistant
To modify your code to use option 3 (Reading in chunks), you'll want to approach each CSV file individually, process it in chunks, and then append each chunk to your list of dataframes before finally concatenating them at the end. Here's how you could do it:
import pandas as pd import os csv_directory = r"E:\01_calculate_talib\day_spot" csv_files = [file for file in os.listdir(csv_directory) if file.endswith('.csv')] data_frames = [] chunk_size = 5000 # Adjust this size based on what your system can handle for csv_file in csv_files: file_path = os.path.join(csv_directory, csv_file) # Process each file in chunks for chunk in pd.read_csv(file_path, chunksize=chunk_size): data_frames.append(chunk) # Now you can concatenate all the processed chunks combined_df = pd.concat(data_frames, ignore_index=True) combined_csv_path = r'C:\Users\Fazel\Desktop\combined_day_stables_1_7_24_4_w_m_labeled.csv' combined_df.to_csv(combined_csv_path, index=False) print(f'Combined CSV created at: {combined_csv_path}')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.