i have a large csv file which im trying to separate , change my code to fix the error:…
- User
i have a large csv file which im trying to separate , change my code to fix the error: import pandas as pd
Replace ‘your_large_file.csv’ with the path to your large CSV file
input_csv = r’C:\Users\arisa\Desktop\combined_day_fl_1hot_custom_extra_and_indic.csv’
This will be the new file with 0.06% of the original data
output_csv = r’C:\Users\arisa\Desktop\combined_day_fl_1hot_custom_extra_and_indic_val.csv’
Adjust this variable for different percentages
fraction_of_interest = 0.06 / 100 # 0.06%
Read the large CSV file
df = pd.read_csv(input_csv)
Sample 0.06% of the rows randomly without replacement
sampled_df = df.sample(frac=fraction_of_interest, random_state=1) # random_state for reproducibility
Write sampled rows to a new CSV file
sampled_df.to_csv(output_csv, index=False) # index=False to avoid writing row indices
print(f’Successfully saved {len(sampled_df)} rows to “{output_csv}”‘)
error: C:\Users\arisa\AppData\Local\Programs\Python\Python310\python.exe C:\Users\arisa\Desktop\train\seprate_train_test.py Traceback (most recent call last): File "C:\Users\arisa\Desktop\train\seprate_train_test.py", line 13, in df = pd.read_csv(input_csv) File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1026, in read_csv return _read(filepath_or_buffer, kwds) File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 626, in _read return parser.read(nrows) File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1968, in read df = DataFrame( File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 778, in init mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager) File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 503, in dict_to_mgr return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy) File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 152, in arrays_to_mgr return create_block_manager_from_column_arrays( File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 2144, in create_block_manager_from_column_arrays mgr._consolidate_inplace() File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 1788, in _consolidate_inplace self.blocks = _consolidate(self.blocks) File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 2269, in _consolidate merged_blocks, _ = _merge_blocks( File "C:\Users\arisa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\managers.py", line 2301, in _merge_blocks new_values = new_values[argsort] numpy.core._exceptions.MemoryError: Unable to allocate 6.09 GiB for an array with shape (2711, 301617) and data type float64
- Assistant
The error you're encountering is a
MemoryErrorbecause Panda's attempt to load your large CSV file into memory exhausts all available memory.This is a common challenge when dealing with large datasets. One way to handle this is to process the file in chunks rather than attempting to load it all at once.
You can adjust your script to process chunks of the CSV file, sample from each chunk, and then concatenate the samples. This way, you can significantly reduce memory usage.