Location>code7788 >text

Python moves the data in certain columns of a table file up one row as a whole.

Popularity:612 ℃/2024-09-09 14:00:12

  This paper describes a system based onPythonlanguage for afile (paper)lowerExcelForms file, for which theEach fileAdd to the operation - to put one of theSpecified columnsare shifted up one line, and all the operated-outExcelThe data in the table file is merged to produce a newExcelThe method of documentation.

  First, let's clarify the requirements of this paper. In a folder with a large number ofExcelForm Files (in.csvformat files, for example), each of which has data characteristics similar to those shown below; we would expect that, for the columns in the purple box in the figure below, where theData section(Each column has alistingsThis listing doesn't count.Data section) are moved up one line (e.g., the first line of the original data section of the2change from line 1 to line 21line, originally No.3change from line 1 to line 22(rows, and so on).

image

  As can also be seen by the above graph, theColumns to be manipulated with dataSome of them are in the original data section of the first1There is no data on the rows, and some of them are in the original data section at paragraph1rows also have data; for the latter, after we move the data up one row, the equivalent of the original first1The data on the line is then overwritten. In addition, it is clear that at the end of each file operation, theColumns processedof the data portion of thelast lineThere must be no data, so before merging the files after all the operations, it is also desirable to merge each of thepost-operation file(used form a nominal expression)last lineDelete.

  Knowing the requirements, we can start writing the code; the specific code is as follows.

# -*- coding: utf-8 -*-
"""
Created on Fri May 19 01:47:06 2023

@author: fkxxgis
"""

import os
import pandas as pd

original_path = "E:/01_Reflectivity/25_2022Data_New"
result_path = "E:/01_Reflectivity/99_Model/02_Extract_Data/26_Train_Model_New"

result_df = ()

for file in (original_path):
    if (".csv"):
        
        df = pd.read_csv((original_path, file))
        columns_move_index = list(range(8, 16)) + list(range(17, 36))
        for columns_index in columns_move_index:
            for i in range(len(df) - 1):
                [i, columns_index] = [i + 1, columns_index]
        if len(df):
            df = (len(df) - 1)
        # df = [ : , 1 : ]
        result_df = ([result_df, df])
        
result_df.to_csv((result_path, "Train_Model_0715_Main.csv"), index = False)

  Among them.original_pathIndicates that there are multiple pendingExcelthe folder path of the form file.result_paththen the resultExcelThe path where the form file is stored.

  First of all, we pass theresult_df = ()Create an emptyDataFrame, which is used to save the processed data. Next, iterate through all the files in the original folder and find the file in the folder starting with.csvending files; subsequently, reading these.csvfile and save it to thedfCenter.

  Secondly, we have been able to achieve this through thecolumns_move_index = list(range(8, 16)) + list(range(17, 36))Specify the index range of the columns where the data needs to be moved and subsequently iterate through the columns where the data needs to be moved. The next[i, columns_index] = [i + 1, columns_index]Indicates that the data in the current row is replaced with the corresponding data in the next row.

  Next, we pass theif len(df):determine whetherDataFrameNot empty, if so deleteDataFramein the last row of data; subsequently, the processedDataFrameconnect toresult_dfCenter.

  Lastly, we have passed theresult_df.to_csv()function that takes the final processedDataFrameSave as a newExcelform documents so as to fulfill our needs.

  At this point, the job is done.