Python Pandas - Create a new CSV header line without reading / writing the entire file

I have a 27 GB CSV file and I just want to rename the header lines. Can I do this without reading the entire file in a data framework and then writing the entire file again?

This is essentially what I want to do, but without re-writing the entire 27 gigabyte file.

data = pd.read_csv(filename,sep="|",nrows=2)
data.head()

LOC_ID  UPC FW  BOP_U   BOP_$
0   17  438531560821    201712  1   40.0
1   239 438550152328    201719  2   28.8


data.columns = ['WHSE','SKU','PERIOD','QUANTITYONHAND','DOLLARSONHAND']
data.head()


   WHSE           SKU  PERIOD  QUANTITYONHAND  DOLLARSONHAND
0    17  438531560821  201712               1           40.0
1   239  438550152328  201719               2           28.8
+4
source share
1 answer

Just indicate that there is only one line with nrows.

header_df = pd.read_csv('my_file.csv', index_col=0, nrows=1)

As for re-writing the file, I don't think you have to process the whole file for re-writing.

+1
source

Source: https://habr.com/ru/post/1670088/


All Articles