


Python BOT extract long column from Excel sheet and create a dataframe to catalog some numbers from another file
I need to create a Python robot to extract column C from Excel file 1, sheet 1, and catalog it in file 2 , and calculates the sum of numbers from 0.00 to 0.99, from 1.00 to 1.99, etc. 12. All numbers above 12 are coded into the last row. Then I need to calculate the sum of all numbers.
I tried writing some code but it didn't write anything on the Excel file.
Correct answer
You can try the following methods;
- Read the excel data file (excel file 1) and select only the required columns ("column c").
- Create an array of values 0.00 - 0.99, 1.00 - 1.99, 2.00 - 2.99, 3.00 - 3.99 (up to 12) and use it to create a new data frame (df_write), grouping the values in the data frame into array ranges Inside. Get the count for each range.
- Count values greater than 12 and add them to df_write as new rows.
- Sum all values in the data frame and add them to df_write as new rows.
- Write data frame to excel. In the example, xlsxwriter is used as the engine, which means that the workbook (catalog file) is created/overwritten every time the code is run.
- Additional data/formats can be included in the table. For example, change the text in the cell and add a formula to calculate the total number of all grouped range values, which should be equal to the total number of rows read from the excel data file (datafile).
import pandas as pd datafile = "Excel File 1.xlsx" catalogfile = 'Excel File 2.xlsx' column = 'column C' ### Read specific column (column) from Excel Sheet df_read = pd.read_excel(datafile, index_col=None, na_values=['NA'], usecols=[column]) # print(df_read) ### Create the dataframe of values within specified ranges to write to Excel ### Group ranges 0.00 - 0.99 in increments of 1 and make a count of each up to a max (12) df_write = df_read.groupby(pd.cut(df_read[column], [float(i) - 0.01 for i in range(0, 13)])).count() ### Count values greater than 12 and add as row to the dataframe df_write.loc['12+'] = df_read[df_read > 12].count() ### Sum all values in the column and add as row to the dataframe df_write.loc[len(df_write.index) + 1] = df_read.sum() ### Rename Index Header df_write.index.name = 'Range Totals' ### Rename Column Header df_write.columns = ['Values Count'] ### Write dataframe to Excel ### Using default engine Xlsxwriter so new workbook is created (any existing workbook is overwritten) with pd.ExcelWriter(catalogfile) as writer: df_write.to_excel(writer, sheet_name='Sheet1', index=True) ### Xlsxwriter formatting workbook = writer.book cell_format = workbook.add_format() cell_format.set_bold(True) ws = writer.sheets['Sheet1'] ### Rename Row Header and add formula to count the totals for each range ### (should equal the total number of data rows read from Excel) ws.write(df_write.size, 0, 'Column Total', cell_format) ws.write_row(df_write.size + 1, 0, ['Total Rows', '=SUM(B2:B14)'], cell_format) ws.autofit()
Example of what an excel worksheet looks like for a column containing 100 rows of data (i.e. excluding hader) read from a data file.
The Range Total column is an index column in the data frame. The range text is determined by the data frame, but actually covers the ranges 0.00 - 0.99, 1.00 - 1.99, 2.00 - 2.99, 3.00 - 3.99, etc.
If needed you can remove the index column from the dataframe when writing to excel and use xlsxwriter to write custom text to the column or use a template with existing headers (in this case excelwriter requires additional schema and openpyxl as engine writes to an existing workbook).
The above is the detailed content of Python BOT extract long column from Excel sheet and create a dataframe to catalog some numbers from another file. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

This article guides Python developers on building command-line interfaces (CLIs). It details using libraries like typer, click, and argparse, emphasizing input/output handling, and promoting user-friendly design patterns for improved CLI usability.

This tutorial builds upon the previous introduction to Beautiful Soup, focusing on DOM manipulation beyond simple tree navigation. We'll explore efficient search methods and techniques for modifying HTML structure. One common DOM search method is ex
