SoFunction
Updated on 2024-12-10

Python implementation of reading Excel files and copying the specified rows of data

This paper describes a system based onPythonLanguage, readExcelform file data and based on whichThe value of a column of datawillThis data is in the specified range(used form a nominal expression)that lineCopy it and save the result as a newExcelMethods for form documents.

First, let's clarify the specific needs of this paper. There is an existingExcelform file, in this article we'll take the.csvformat as an example; where, as shown below, there is a column in this file (that is, theinf_dif(This column) data is more critical, and we want to treat this column - for theeach lineIfThe value of this column of data in this rowwithin the specified range, then copy the line (equivalent to generating a newand the current lineIt's the same data.new line)。

Knowing the requirements, we can start writing the code. Among them, the specific code used in this article is shown below.

# -*- coding: utf-8 -*-
"""
Created on Thu Jul  6 22:04:48 2023

@author: fkxxgis
"""

import pandas as pd

df = pd.read_csv(r"E:\Train_Model.csv")

result_df = ()
for index, row in ():
    value = row["inf_dif"]
    if value <= -0.1 or value >= 0.1:
        for i in range(10):
            result_df = result_df.append(row, ignore_index=True)
    result_df = result_df.append(row, ignore_index=True)

result_df.to_csv(r"E:\Train_Model_Oversampling_NIR_10.csv", index=False)

Among other things, the details of the above code are described below.

First, we need to import the required libraries; next, we use thepd.read_csv()function that reads the file we need to process and subsequently stores the data in a file nameddf(used form a nominal expression)DataFramein the format variable. Next, we create an emptyDataFrameThe name isresult_df, which is used to store the processed data.

Subsequently, we use the()Iterate through each row of the original data, whereindexindicateline indexrowimitateThis line of specific data. Next, get each line in theinf_difThe value of the column, stored in the variablevalueCenter.

At this point, we can use the variables based on our actual needs.valuevalues to be judged; in my case, if thevalueis less than or equal to the value of-0.1or greater than or equal to0.1and start copying the line; since I need to copy it more often here, I'll use therange(10)loop that copies the current row of data10times; replication is specified by using theresult_df.append()function, which adds the copied lines to theresult_dfCenter.

Finally, care needs to be taken to useresult_df.append()function that adds the raw row data to theresult_df(which is equivalent to adding the line we need to the one we just copied to the10times, a total of11(line up).

In the last step, we use theresult_df.to_csv()function, which saves the resultant data after processing as a newExcelform document file and set theindex=False, indicates that the row index is not saved.

Run the above code, we can get the result file. As shown below, you can see that the lines in the result file, which meet our requirements, have been copied10times, that is, a total of11Times.

At this point, the job is done.

This article on Python to read Excel files and copy the specified data line of the article is introduced to this, more related Python to read Excel and copy the contents of the search for my previous posts or continue to browse the following related articles I hope you will support me in the future!