iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python BOT 从 Excel 工作表中提取长列并创建一个数据框来对另一个文件中的一些数字进行编目
  • 598
分享到

Python BOT 从 Excel 工作表中提取长列并创建一个数据框来对另一个文件中的一些数字进行编目

2024-02-10 19:02:17 598人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

问题内容 我需要创建一个 python 机器人,从 excel 文件 1、工作表 1 中提取 C 列,并在文件 2 中进行编目,并计算从 0.00 到 0.99、从 1.00 到 1.

问题内容

我需要创建一个 python 机器人,从 excel 文件 1、工作表 1 中提取 C 列,并在文件 2 中进行编目,并计算从 0.00 到 0.99、从 1.00 到 1.99 等的数字总和。 12. 所有 12 以上的数字都编入最后一行。然后我需要计算所有数字的总和。

我尝试编写一些代码,但它没有在 Excel 文件上写入任何内容。


正确答案


您可以尝试以下方法;

  1. 读取 excel 数据文件(excel 文件 1),仅选择所需的列(“c 列”)。
  2. 创建值 0.00 - 0.99、1.00 - 1.99、2.00 - 2.99、3.00 - 3.99(最多 12 个)的数组,并使用它创建一个新数据帧 (df_write),将数据帧中的值分组到数组范围内。获取每个范围的计数。
  3. 对大于 12 的值进行计数,并将其作为新行添加到 df_write。
  4. 对数据帧中的所有值求和,并将其作为新行添加到 df_write。
  5. 将数据框写入 excel。在示例中,xlsxwriter 用作引擎,这意味着每次运行代码时都会创建/覆盖工作簿(目录文件)。
  6. 表格中可以包含其他数据/格式。例如,更改单元格中的文本并添加公式来计算所有分组范围值的总数,该总数应等于从 excel 数据文件(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()

对于从数据文件读取的包含 100 行数据(即排除 hader)的列,excel 工作表的外观示例。
“范围总计”列是数据框中的索引列。范围文本由数据框确定,但实际上涵盖范围 0.00 - 0.99、1.00 - 1.99、2.00 - 2.99、3.00 - 3.99 等。
如果需要,在写入 excel 时可以从数据框中删除索引列,并使用 xlsxwriter 将自定义文本写入列,或者使用具有现有标题的模板(在这种情况下,excelwriter 需要附加模式和 openpyxl 作为引擎写入现有工作簿)。

以上就是Python BOT 从 Excel 工作表中提取长列并创建一个数据框来对另一个文件中的一些数字进行编目的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: Python BOT 从 Excel 工作表中提取长列并创建一个数据框来对另一个文件中的一些数字进行编目

本文链接: https://www.lsjlt.com/news/563199.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作