Optimal way to “rotate” contents of a datafile?

I have very large datafiles (csv files that are several GB a piece, roughly a 3059 by 40-80k long table) that are used for constructing images from spectroscopy data. They’re standard tab delimited files, where the first list column is the x-axis values, and each consecutive column is the associated values for each pixel. Like such for tens of thousands of columns:

900.00 0.000 …
901.00 0.030 …
… … …
3999.00 0.801 …
4000.00 0.798 …

Normally, I can get these files as datapoint tables and have written a library with a bunch of functions and operations using the data (important detail: this whole library is column based). Recently, I had to explore the idea of manually converting this data from its native format. In that format, the non-x-axis values are stored as a comma delimited string which contains all the data end to end (so a single string a couple hundred thousand entries long). Such as:

(0.0234,0.0021,…,0.4120,0.3034)

I can currently extract the data and write it to a file, but because you can only write from left to right top to bottom and cant write ‘vertically’ to a file, I write the data row-based. Since the rest of my library operates on column-based data, I wanted to ‘rotate’ or ‘tip-over’ the contents of the file to the left so the columns are now the rows and plays nicely with the rest of my code rather than rewriting my library to be row based.

Here is what I have written:

def FileRotate(inputfile):
  global outputfile
  outputfile = targetFiles[fileIndex] + "_final.dpt"
  # outputfile = "/path/to/input/file.txt"

  with open(outputfile, 'w') as output_file:
    outputcsv = csv.writer(output_file, delimiter = 't')
    ncols = len(pd.read_csv(inputfile, nrows=1, encoding='utf-8', delimiter='t').columns)
    print(f'nNumber of columns which will become number of rows: {ncols}')
    for i in (iterable :=list(range(0,ncols))):
      columntemp = pd.read_csv(inputfile, usecols=[i], encoding='utf-8', delimiter='t', header=None)
      outputcsv.writerow(columntemp.iloc[:,0])
      if i < iterable[-1]:
        print(f'Column {i} has successfully been written. Beginning column {i+1}')
      elif i == iterable[-1]:
        print(f'nColumn {i} has successfully been written. File is complete.')

I’m very new to programming so I don’t think the code is the best, hence why I’m reaching out to you all. Currently, my strategy is, while iterating over the string of native data and putting it in x-axis sized chunks, to read one column of the input file (using pandas since it can read specific columns) and then write that column to the new file, so it would now be a row. The problem is, at its longest this rotation could take several hours. Is there an easy/more computationally savvy way of accomplishing this task? I’m also—-of course–open to new ways of approaching this problem as well. I couldn’t care less about the if and printing, thats just so I have some idea of the internals and the pace at which it was running. the declaration of outputfile as global is also for the rest of the code not shown.

New contributor

Levi Friss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật