Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Excel交叉乘法_Python_Excel - Fatal编程技术网

Python Excel交叉乘法

Python Excel交叉乘法,python,excel,Python,Excel,我正在尝试制作一个简单的python脚本,它将在excel中交叉乘以两列。例如: 第一列是A、B、C,第二列是D、E、F。我希望第3+4列将两列交叉相乘 迄今为止的代码: import openpyxl wb = openpyxl.load_workbook('list.xlsx') sheet = wb.get_sheet_by_name('Sheet') i=1 maxRow = len(sheet['B']) for rowNum in sheet.columns[1]:

我正在尝试制作一个简单的python脚本,它将在excel中交叉乘以两列。例如:

第一列是A、B、C,第二列是D、E、F。我希望第3+4列将两列交叉相乘

迄今为止的代码:

import openpyxl


wb = openpyxl.load_workbook('list.xlsx')
sheet = wb.get_sheet_by_name('Sheet')

i=1
maxRow = len(sheet['B'])

for rowNum in sheet.columns[1]:
    for rowNum2  in sheet.columns[2]:
    sheet.cell(row=i,column=3).value=sheet.cell(row=rowNum,column=1)
    sheet.cell(row=i,column=4).value = sheet.cell(row=i%maxRow,column=2)
    i += 1

wb.save('updatedlist.xlsx')
得到maxRow长度的最佳方法是什么,这样我就可以除以模了


谢谢

如果您想要的是A列值和B列值的所有排列,我建议您阅读这两列,然后直接计算它们:

# use pandas to read in the excel file
import pandas as pd
df = pd.read_excel("filename.xlsx")

# get the permutations (assumes your columns have headers "A" and "B" respectively)
import itertools
result = list(itertools.product(df["A"].values, df["B"].values))

# write back out to a file
output = pd.Series(results).apply(pd.Series)
output.to_excel("result.xlsx")

您不能根据值停止的时间检测最大行吗?不管怎样,你有没有考虑过使用熊猫?它可以很快解决这样的问题我不确定你的API,但是当我使用POI时,我必须在第一个单元格中查找一行内容为空的行,并将其称为“end”,并使用我遇到的条目数作为计数。这不是rowNum循环的功能吗?.Traceback:File“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/index/base.py”,第3078行,在get_loc return self.\u engine.get_loc(key)文件“pandas/\u libs/index.pyx”,在pandas.index.pyx中的第140行。\u libs.IndexEngine.get_loc文件“pandas/\u libs/index.pyx”,第162行,在pandas中。_libs.index.IndexEngine.get_loc File“pandas/_libs/hashtable_class_helper.pxi”,第1492行,在pandas中。_libs.hashtable.PyObjectHashTable.get_项文件“pandas/_libs/hashtable_class_helper.pxi”,第1500行,在pandas中。_libs.hashtable.PyObjectHashTable.get_项keyrerror:“B”您的文件实际上是什么样的?出现此错误是因为没有标题名为“B”的列。默认情况下,Pandas使用文件中的第一行作为标题。如果您的文件没有标题,它将使用数字列名,因此您将分别用df[0]和df[1]替换df[“A”]和df[“B”]。如果您可以在Excel中发布文件的屏幕截图,这将非常有用。如果它只是原始屏幕截图的前两列,那么使用我提到的df[0]和df[1]