如何使用python

时间:2017-07-06 10:31:40

标签: python excel pandas openpyxl xlrd

我想从另一列中使用单元格值减去一列中的单元格值,并将总和写入excel文件中的新列。然后我希望将总和(如果不等于0)添加到列表中供以后使用。我的excel文件中的数据结构如下:

Name | Number | Name1 | Number1
Name2 | Number2 | Name3 | Number3
....
Namex | Numberx | Namey |Numbery

我想从彼此中减去数字,然后将总和添加到新列中,如下所示:

Name| Number | Name1 | Number1 | Sum of (Number - Number1)

我曾尝试使用openpyxl来做这件事,但我真的很困惑,因为文档与早期版本的Python有所不同。我在Python 3.4中工作。我很高兴得到您建议我使用哪个模块的建议。 到目前为止我的代码给了我错误,因为我将excelfile称为生成器,而不是可订阅者。我不确定如何搜索和阅读excelfile,同时使其可订阅,以便可以写入它。有人可以帮帮我吗?

这是我的代码:

from openpyxl import Workbook, load_workbook

def analyzexlsx(filepath):
    numbers = []
    excel_input = load_workbook(filepath)
    filepath = [pth for pth in Path.cwd().iterdir()
                  if pth.suffix == '.xlsx'] #Want to iterate through several excel files in a folder.
    ws = excel_input.active
    cols = tuple(ws.columns)
    col_b = cols[1] 
    col_e = cols[4] 
    for j, k in zip(col_e, col_b): 
        if None:
            print('None')
        equally = (int(j.value) - int(k.value)) #line 13, error. Trying to subtract column cell values.
        if equally != 0: #If the columns sum is not equal to 0, it is to be added to the numbers list.
            numbers.append(j.row)

        else:
            pass

    col1 = []
    col2 = []
    col4 = []
    col5 = []
    col7 = []
    col8 = []

    mainlist = []
    try:
        for row in numbers:
            col1.append(str(ws.cell(row=row, column=1).value))
            col2.append(str(ws.cell(row=row, column=2).value))
            col4.append(ws.cell(row=row, column=4).value)
            col5.append(ws.cell(row=row, column=5).value)
            col7.append(ws.cell(row=row, column=7).value)
            col8.append(ws.cell(row=row, column=8).value)
    finally:
        for i, j, k, l, m, n in zip(col1, col2, col4, col5, col7, col8):
            mainlist.append(i + ", " + j + ", " + k + ", " + l + ", " + m + ", " + n)
    return mainlist

Traceback (most recent call last):
    Line 13, in analyzexlsx
        equally = (int(j.value) - int(k.value))
    TypeError: int() argument must be a string or a number, not 'NoneType

我会很高兴得到答案,因为我已经研究了很长一段时间,现在我被卡住了。我对Python很新。

1 个答案:

答案 0 :(得分:3)

首先通过read_excel从excel创建DataFrame

然后需要使用2.列减去4

df = pd.read_excel('file.xlsx')

#select by column name
df['E'] = df['B'] - df['D']
#select by positions, but python count from 0 so for 2. column need 1
df['E'] = df.iloc[:, 1] - df.iloc[:, 3]

也许还有助于检查documentation