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 文件上写入任何内容。
您可以尝试以下方法;
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文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0