Reading specific rows from CVS to Excel - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Reading specific rows from CVS to Excel (/thread-22198.html) |
Reading specific rows from CVS to Excel - DavidTheGrockle - Nov-03-2019 I am not completely sure that this is a Python pandas problem. I start from a CVS file with 707,000 rows. From this (with help from yourselves) I have found out the first and last rows associated with a given ID in column 9. I have transferred these limits into an Excel workbook. For example : The col 9 ID EG47296 runs from row 3833 to row 13896. Now I want to transfer these EG47296 rows from the CVS file into my new Excel workbook and particularly into worksheet(EG47296). It might be possible to copy paste, but I would rather do it programatically as there are so many rows. How to set about this, please ? RE: Reading specific rows from CVS to Excel - klllmmm - Nov-03-2019 May be you can refer to this sample code import pandas as pd from pandas import ExcelWriter from copy import deepcopy data = pd.read_csv('c://filepath.csv') #Set the CSV file path # Select the required columns, rows from CSV data1 = data.iloc[3832:13896,['column 9']].copy() # Set the required rows, columns and get a copy # Save to excel writer = ExcelWriter('c://filepathtoexcel.xlsx') data1.to_excel(writer,'EG47296',index=False) writer.save() RE: Reading specific rows from CVS to Excel - DavidTheGrockle - Nov-05-2019 Working from your code I have got the data out of the CSV file, because I can see it on the clipboard. But I still cannot get it to go into the Excel file. I have been fiddling with the syntax but, so far, to no avail. The error message seems to indicate that a module openpyxl is missing. What I have is # This is to read from row_s to row_e in Padget.cvs # and to put the material in Pajb.xlsm import pandas as pd from pandas import ExcelWriter from copy import deepcopy print("Hello, World!") url = "F:/Carrier Bag F/NAV Padget/Padget.csv" pacto = pd.read_csv(url) # Select the required columns, rows from CSV #data1 = pacto.iloc[24120:24130,['column 1' : 'Column 9']].copy() data1 = pacto.iloc[24120:24130, 1:9].copy() # A check #data1.to_clipboard() # THIS was OK I can see the data on the clipboard # Save to excel vrl = "E:/CSVStuff/Pajb.xlsm" writer = ExcelWriter(vrl) #data1.to_excel(writer, sheet_name= 'EG47296',index=False, startrow=1, startcol = 1) data1.to_excel(writer, sheet_name= 'EG47296',index=False) # Doesn't work. writer.save() RE: Reading specific rows from CVS to Excel - DavidTheGrockle - Nov-06-2019 OK Got it to work. Thank you. |