SoFunction
Updated on 2024-11-14

Python implementation of the table file to find the maximum value of a region of cells

This paper describes a system based onPythonLanguage, based onExceldata in a column within the table file, calculating the data in that column for each of theSpecified number of rowsrange (e.g., every4(within the scope of the line) of theInterval MaximumThe methodology.

It is known that we have an existing.csvformalizedExceltable file that has a column of data that we want to add to theInterval Maximumof the calculation - i.e., from this column of theData section(a.k.a.Excluding listings(part of) beginning with the first1go to the end of the line4The maximum value between rows, the first5go to the end of the line8The maximum value of the line, the first9go to the end of the line12The maximum value of a row, etc., is calculated separately for each4the maximum value in the row; moreover, if the number of data in this column can not be4Divide, then how many are left at the end, then just maximize those.

Once the requirements are clear, we can start writing the code; this is shown below.

# -*- coding: utf-8 -*-
"""
Created on Wed Jul 26 12:24:58 2023
@author: fkxxgis
"""
import pandas as pd
def calculate_max_every_eight_rows(excel_file, column_name):
    df = pd.read_csv(excel_file)
    column_data = df[column_name]
    max_values = []
    for i in range(0, len(column_data), 4):
        max_values.append(column_data[i:i+4].max())
    return max_values
excel_file = r"C:\Users\15922\Desktop\data_table_1.csv"
column_name = 'NDVI'
result = calculate_max_every_eight_rows(excel_file, column_name)
rdf = (result, columns = ["Max"])
output_file = r"C:\Users\15922\Desktop\"
rdf.to_csv(output_file, index = False)

Here, we define a functioncalculate_max_every_eight_rows(Because at first I was trying to calculate8The interval maximum of the data for all functions with the nameeight(if you understand it), accepts two parameters, which are the path to the input fileexcel_fileand the name of the column that corresponds to the maximum value of the interval to be calculated.column_name

In the function, we first read the file and save the data to thedfNext, we get the specified columncolumn_nameand create an empty listmax_values, which is used to save the maximum value for each grouping. Subsequently, using therangeThe function generates the data from the0Starting with a step size of4The sequence of indexes to be used for each4The rows are grouped together; here you can modify them according to your actual needs. Within each grouping, we start with thecolumn_datato retrieve the corresponding4rows of data and calculates the maximum value within that grouping, adding the maximum value to themax_valuesin the list. Finally, the function returns the list holding the maximum value for each groupingmax_values

Secondly, we have been able to achieve this through theexcel_fileSpecify the path to the input file, via thecolumn_nameSpecify the names of the columns to be processed, and then you can invoke thecalculate_max_every_eight_rowsfunction and saves the returned result to theresultvariable, the result is a list containing the maximum value for each grouping.

Subsequently, we chose to save the maximum result in order to save it, so we chose to set theresultThe list is converted to a newDataFrameformat datardfand specify the column name asMax. Finally, byrdf.to_csv(): put thisrdfSave as a new.csvformat file and set theindex=Falseto not save indexed columns.

Execute the above code, we can get the result file. As shown in the figure below, in order to facilitate the comparison, we here will copy the results file to the original file to view. You can see that the result column in the first1numbers, which are the first two numbers in the original column4The maximum value of the row; the first value in the result column3number, then it is the first number in the original column9travel to12The maximum value of the row, and so on.

At this point, the job is done.

Above is Python to achieve a form file to find the maximum value of a region of the cell details, more information about Python to find the maximum value of the file please pay attention to my other related articles!